1

I am currently working on a project in Python. To create the GUI, I used PyQt5 Designer. (The pictures I will link NOT be my project, I just try to make this feature work in a separate dummy program, cause I have the same problem in my project). So let's suppose I have a simple widget with a label on it, nothing special. This is the UI file in PyQt5 Designer This is the UI file in PyQt5 Designer

If I load the file with uic.loadUi, it works fine. This is the code.

from PyQt5.QtWidgets import QMainWindow, QApplication, QSlider, QLabel, QDialog
from PyQt5 import uic
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSignal
import sys



class UI(QDialog):
    def __init__(self):
        super(UI, self).__init__()

        # Load the ui file
        uic.loadUi("testlabel.ui", self)

        # Show The App
        self.show()
        

# Initialize The App
app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()

When i run the program, it does exactly what I want, loads the ui file. When i run the program, it does exactly what I want, loads the ui file.

But I couldn't made the label clickable, therefore I started to search for a solution, and read that I have to make a subclass of QLabel and etc. So I tried to do this. So in PyQt Designer I clicked on promote to, and filled the empty rows like this

and added it

and added it

Now the label became a ClickableLabel. You can see it here..

And in the code file I modified the code like this.

from PyQt5.QtWidgets import QMainWindow, QApplication, QSlider, QLabel, QDialog
from PyQt5 import uic
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSignal
import sys

class ClickableLabel(QLabel):

    clicked = pyqtSignal()
    def __init__(self, parent=QLabel):
        QLabel.__init__(self, parent=parent)

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

class UI(QDialog):
    def __init__(self):
        super(UI, self).__init__()

        # Load the ui file
        uic.loadUi("testlabel.ui", self)

        # Show The App
        self.show()
        

# Initialize The App
app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()

In the code defined the ClickableLabel class and used a pyqtSignal(), with "emit". But after running the code, I received an error message like this. this

Is there any way I can solve my problem? (I would like to keep the design I created in PyQt5 Designer)

Thank you in advance for the answer.

Tomi Oláh
  • 23
  • 4
  • class `ClickableLabel` definition should be in file `ClickableLabel.py` and both class name and header of promoted widget should be `ClickableLabel`, demote remove add and promote it again – mugiseyebrows Mar 02 '23 at 15:25
  • You can just use QPushButton and set its `flat` property to true in designer, by the way – mugiseyebrows Mar 02 '23 at 15:28
  • @mugiseyebrows Finally Works, I needed to change the header file to a '*.py' file name without '.py', which contains the class. And I had to import it to testlabel.py like from clickable_label import ClickableLabel Thank you for answers. – Tomi Oláh Mar 02 '23 at 18:18
  • @TomiOláh Note that you can follow the suggestion from mugiseyebrows and use a button, then just set a basic stylesheet for it (`background: #90ee90; border: none`). The difference is that the `clicked` signal of buttons is only emitted when the *left* mouse button is *released* within its area; if you want it to react to *any* button, you need a custom widget (or, at least, an event filter) as you're doing, otherwise, you can use a normal button styled as written above, but connected the [`pressed`](https://doc.qt.io/qt-5/qabstractbutton.html#pressed) signal instead. – musicamante Mar 02 '23 at 20:49

0 Answers0