Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clickable event on QLabel in python using pyqt4?

Tags:

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.

like image 465
Zeb Avatar asked Mar 06 '14 18:03

Zeb


People also ask

What is Python qt4?

PyQt4 is a comprehensive set of Python bindings for Digia's Qt cross platform GUI toolkit. PyQt4 supports Python v2 and v3.

What is QLabel in pyqt5?

A QLabel object acts as a placeholder to display non-editable text or image, or a movie of animated GIF. It can also be used as a mnemonic key for other widgets. Plain text, hyperlink or rich text can be displayed on the label.

What can you do with PyQt?

PyQt is a Python binding for Qt, which is a set of C++ libraries and development tools that include platform-independent abstractions for Graphical User Interfaces (GUI), as well as networking, threads, regular expressions, SQL databases, SVG, OpenGL, XML, and many other powerful features.


Video Answer


2 Answers

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): ... 
like image 182
qurban Avatar answered Sep 28 '22 12:09

qurban


QLabel doesn't have a signal clicked, so you can do one of the following:

A) Derive a custom class from QLabel implementing handlers for mouse events.

B) Implement the event handlers in Ui_Form, using standard QLabels and install the form as an event filter for the labels (self.QPLabel.installEventFilter (self)).

like image 35
Hyperboreus Avatar answered Sep 28 '22 11:09

Hyperboreus