PyQt5--CustomizeSignal
1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 14, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: This example is show how to triggered the customize signal. 8 创建了一个名为closeApp的信号。这个信号会在按下鼠标时触发,它连接着QMainWindow的close()插槽 9 ''' 10 import sys 11 from PyQt5.QtCore import pyqtSignal,QObject 12 from PyQt5.QtWidgets import QApplication,QMainWindow 13 14 class Communicate(QObject): 15 closeApp = pyqtSignal() # 信号closeApp是Communicate的类属性,它由pyqtSignal()创建。 16 17 class New_test(QMainWindow): 18 def __init__(self): 19 super().__init__() 20 self.initUI() 21 22 def initUI(self): 23 self.c = Communicate() 24 self.c.closeApp.connect(self.close) # 自定义closeApp信号连接到QMainWindow的close槽 25 26 self.setGeometry(300,300,250,150) 27 self.setWindowTitle('Emit Signal') 28 self.show() 29 30 def mousePressEvent(self,event): 31 self.c.closeApp.emit() # 当在窗体上点击鼠标时会触发closeApp信号,使程序退出 32 33 if __name__ == '__main__': 34 app = QApplication(sys.argv) 35 ex = New_test() 36 sys.exit(app.exec_())
当程序运行后,在窗口中点击鼠标键时该窗口就会自动退出