5

I have a Qlabel filled with QPixmap and I want to start a process/function once this label clicked. I had extended QLabel class as follows:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class QLabel_alterada(QLabel):
  clicked=pyqtSignal()
  def __init(self, parent):
    QLabel.__init__(self, QMouseEvent)

  def mousePressEvent(self, ev):
    self.clicked.emit()

Then, in my pyuic5-based .py file (I used QtDesigner to do the layout) after importing the module where I save the extended QLabel class, inside the automatically generated setupui, function I changed my Label from

self.label1=QtWidgets.QLabel(self.centralwidget)

to

self.label1 = QLABEL2.QLabel_alterada(self.centralwidget)

Finally, in the core app Python file where I put all the procedures/classes whetever needed to the application functionality I added

self.ui.label1.clicked.connect(self.dosomestuff) 

The application does not crashes but the labels still not clickable. Can someone give me some help on this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Francisco Cunha
  • 355
  • 1
  • 4
  • 14
  • I have posted a answer but if it is not enough you could provide the code generated by QtDesigner where you made the modifications. – eyllanesc Aug 08 '17 at 18:59
  • tks @eyllanesc. It is still unclickable. The unique change that I made in the code generated by QtDesigner was with respect to the Qlabel object that I want to transform into "clickable". I referred this change in my post. Do you think it can be related to "self.centralWidget" as parameter to the QLabel_alterada class? – Francisco Cunha Aug 08 '17 at 19:16
  • Yes, you can do it, I think the error is elsewhere, so I ask you for the code that QtDesigner generates with the changes you have made. – eyllanesc Aug 08 '17 at 19:20
  • You could show the folder structure you use. – eyllanesc Aug 08 '17 at 19:22
  • @eyllanesc I have a unique folder containing both the .py file with code generated by pyuic and the file containing the core app. This folder also contains the file where I save my QLabel extended class. The code from QtDesigner is huge with all the setobjectnames, addwidgets, addlayouts and things like that that qtdesigner+pyuic automatically handle – Francisco Cunha Aug 08 '17 at 19:28
  • You could share the code through github, drive or similar. – eyllanesc Aug 08 '17 at 19:33
  • In the following link is all the code and to make it easy to visualize the QLabel I added a black background: https://gist.github.com/eyllanesc/9432afa8af9cf46ef262c96cfd1ef43e – eyllanesc Aug 08 '17 at 19:51
  • Probe your code and you pass the link, did it work? – eyllanesc Aug 09 '17 at 00:19
  • Your code works for me, just add colors so you can see where to click. – eyllanesc Aug 09 '17 at 15:24
  • You could share the complete code, with images and everything you need to please, to test it and give you my opinion. – eyllanesc Aug 09 '17 at 15:29
  • I already figured it out! I really appreciate your help. Thank you so much – Francisco Cunha Aug 09 '17 at 15:33
  • If my answer helps you please do not forget to mark it as correct please. – eyllanesc Aug 09 '17 at 15:33

4 Answers4

9

I do not understand why you pass QMouseEvent to the parent constructor, you must pass the parent attribute as shown below:

class QLabel_alterada(QLabel):
    clicked=pyqtSignal()

    def mousePressEvent(self, ev):
        self.clicked.emit()

To avoid having problems with imports we can directly promote the widget as shown below:

We place a QLabel and right click and choose Promote to ...:

enter image description here

We get the following dialog and place the QLABEL2.h in header file and QLabel_changed in Promoted class Name, then press Add and Promote

enter image description here

Then we generate the .ui file with the help of pyuic. Obtaining the following structure:

├── main.py
├── QLABEL2.py
└── Ui_main.ui

Obtaining the following structure:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.label.clicked.connect(self.dosomestuff) 

    def dosomestuff(self):
        print("click")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
4

Since Python can pass function as object, I think make a class inherit QLabel only to make it clickable is overkill. Instead I do like this:

import sys

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFrame, QLabel


def foo(*arg, **kwargs):
   print("Bar")


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    Frame = QFrame()
    label = QLabel(Frame)
    label.mousePressEvent = foo
    label.setText("Label")
    Frame.show()
    sys.exit(app.exec_())
0
class ClickableLabel(QtWidgets.QLabel):
    def __init__(self, whenClicked, parent=None):
        QtWidgets.QLabel.__init__(self, parent)
        self._whenClicked = whenClicked

    def mouseReleaseEvent(self, event):
        self._whenClicked(event)

and then:

        my_label = ClickableLabel(self.my_label_clicked)
...
    def my_label_clicked(self, event):
        button = event.button()
        modifiers = event.modifiers()

        if modifiers == Qt.NoModifier and button == Qt.LeftButton:
            logger.debug('my_label_clicked: hooray!')
            return

        LOGGER.debug('my_label_clicked: unhandled %r', event)
Waxrat
  • 510
  • 2
  • 11
0

In PyQT5, you can try to add code in setupUi and create clicked method:

def setupUi()
   ... 
   self.label1.linkActivated.connect(self.clicked)
   ...

def clicked(self):
    print(self.label1.text()) #For example, printing the label text...

Hope this helps!

Clayton Maciel
  • 161
  • 1
  • 4