#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
In this example, we show how to
emit a custom signal.
Author: Jan Bodnar
Website: zetcode.com
Last edited: August 2017
"""
import sys
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication
class Communicate(QObject):
closeApp = pyqtSignal()
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.c = Communicate()
self.c.closeApp.connect(self.close)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Emit signal')
self.show()
def mousePressEvent(self, event):
self.c.closeApp.emit()#这行代码的作用是手动触发(或“发出”)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
我们创建了一个叫 closeApp 的信号,这个信号会在鼠标按下的时候触发,事件与 QMainWindow
绑定。
text
class Communicate(QObject):
closeApp = pyqtSignal()
Communicate
类创建了一个 pyqtSignal()
属性的信号。
text
self.c = Communicate()
self.c.closeApp.connect(self.close)
closeApp
信号 QMainWindow
的 close()
方法绑定。
text
def mousePressEvent(self, event):
self.c.closeApp.emit()
标签:__,QMainWindow,pyqtSignal,self,Communicate,发送,closeApp,信号
From: https://blog.csdn.net/weixin_72050316/article/details/142069019