views:

332

answers:

3

Hi

I've this simple problem, I can grab the event of a click on button, but now I need to handle a click over a widget, here is part of the code:

self.widget = QtGui.QWidget(self)
self.widget.setStyleSheet("QWidget { background-color: %s }" % color.name())
self.widget.setGeometry(150, 22, 50, 50)
self.connect(???)

well, what should i put in the ??? to grab a click action over the created widget? thank in advanced

A: 

See http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#connecting-signals-using-keyword-arguments

self.widget.click.connect(self.onWidgetClick)
Shane Holloway
QWidget doesn't appear to have a `click` attribute.
Xiong Chiamiov
A: 
self.connect(self,QtCore.SIGNAL("clicked"),method_to_call)
tillsten
Widget hasn't the signal clicked()
Lopoc
ups, you are right.
tillsten
+2  A: 

Use mousePressEvent instead.

import sys

from PyQt4.QtGui import QWidget, QApplication

class MyWidget(QWidget):
    def mousePressEvent(self, event):
        print "clicked"

app = QApplication(sys.argv)

widget = MyWidget()
widget.show()

app.exec_()
Jesse Aldridge