
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.QLab

本文介绍了如何在PyQt5中创建一个可点击的QLabel,并在点击时触发特定功能。通过扩展QLabel类并实现mousePressEvent方法,以及使用clicked信号连接处理函数,解决了QLabel无法响应点击的问题。此外,还提供了在QtDesigner中通过Promote to功能将QLabel升级为自定义类的方法。
最低0.47元/天 解锁文章
521

被折叠的 条评论
为什么被折叠?



