I have 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(???) <-- here
What should I put in the "???" to grab a click action over the created widget?
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_()
you can try this i found this from this blog site's comment box from Jared Glass It was working fine for me
self.widget.mouseReleaseEvent=self.myfunction
or
self.widget.mouseReleaseEvent=lambda event:print 'working'
or
self.widget.mouseReleaseEvent=lambda event,my_variable:self.myfunction(event,my_variable)
only the last example is what i have written on my own rest all have been mentioned in http://popdevelop.com/2010/05/an-example-on-how-to-make-qlabel-clickable/ . The last code helps you to pass any variables eg:widget name or widget number if multiple widgets exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With