Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt, click action on Qwidget

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?

like image 373
Lopoc Avatar asked Jan 03 '10 22:01

Lopoc


2 Answers

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_()
like image 174
Jesse Aldridge Avatar answered Oct 14 '22 12:10

Jesse Aldridge


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.

like image 20
Ja8zyjits Avatar answered Oct 14 '22 13:10

Ja8zyjits