clickable event on QLabel in python using pyqt4?

Zeb picture Zeb · Mar 6, 2014 · Viewed 15.4k times · Source

I am working in python GUI using pyqt4 library and new with signal and slots. I don't know how to put event on label name QPLabel. Here is my code :

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(759, 598)
        font = QtGui.QFont()
        font.setPointSize(12)
        ...
        ...
        ...
        self.QPLabel = QtGui.QLabel(Form)
        self.QPLabel.setGeometry(QtCore.QRect(620, 420, 141, 20))
        QtCore.QObject.connect(self.QPLabel, QtCore.SIGNAL(_fromUtf8("clicked()")), self.doSomething)
    def doSomething(self):
         print 'Label click'

Anybody what should I do for event on label for doing some action.

Answer

qurban picture qurban · Mar 6, 2014

Update the following line:

QtCore.QObject.connect(self.QPLabel, QtCore.SIGNAL(_fromUtf8("clicked()")), self.doSomething)

To:

self.QPLabel.mousePressEvent = self.doSomething

and add the event parameter to doSomthing

...
def doSomething(self, event):
...