继承QLabel,编写带有自定义信号的label,通过Qlabel的mouseReleaseEvent相连
class MyQLabel(QtWidgets.QLabel):
# 自定义信号, 注意信号必须为类属性
button_clicked_signal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(MyQLabel, self).__init__(parent)
def mouseReleaseEvent(self, QMouseEvent):
self.button_clicked_signal.emit()
# 可在外部与槽函数连接
def connect_customized_slot(self, func):
self.button_clicked_signal.connect(func)
在主项目中:
label = MyQLabel(central_widget)
label.connect_customized_slot(hide_self)
def hide_self():
label.hide()