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
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.
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
and added it
Now the label became a ClickableLabel. .
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.
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.