这个示例只有这一个python文件。
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QLabel
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QMouseEvent,QFont
class QmyLabel(QLabel):
doubleClicked=pyqtSignal()#自定义信号
def mouseDoubleClickEvent(self, event):
self.doubleClicked.emit()
class QmyWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.resize(280,150)
self.setWindowTitle("Demo5_2,事件与信号")
LabHello=QmyLabel(self)#父容器是self,也就是窗体
LabHello.setText("双击我啊")
font=LabHello.font()
font.setPointSize(14)
font.setBold(True)
LabHello.setFont(font)
size=LabHello.sizeHint()
LabHello.setGeometry(70,60,size.width(),size.height())
LabHello.doubleClicked.connect(self.do_doubleClicked)
def do_doubleClicked(self):
print("标签双击了")
def mouseDoubleClickEvent(self, event):
print("窗体双击事件响应")
if __name__=="__main__":
# QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)#自适应缩放
app=QApplication(sys.argv)
form=QmyWidget()
form.show()
sys.exit(app.exec_())