当前位置:网站首页>信号与槽机制==PYQT5
信号与槽机制==PYQT5
2022-07-25 10:48:00 【栋哥修炼日记】
事件 Event
所有的GUI程序都是事件驱动的。事件主要由用户触发,
但也可能有其他触发方式:例如网络连接、window manager或定时器。
当我们调用QApplication的exec_()方法时会使程序进入主循环。主循环会获取并分发事件。
事件模型中,有三个参与者:
- 事件源:事件源是状态发生变化的对象。它会生成事件。事件(对象)封装了事件源中状态的变动
- 事件对象:事件源对象将事件处理的工作交给事件接收者。
- 事件接受者:事件接收者是要通知的对象
pyqt5有一个独特的signal&slot机制来处理事件。
信号槽用于对象间的通信。signal在某一特定事件发生时被触发,slot可以是任何callable对象。当signal触发时会调用与之相连的slot。
信号和槽机制
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget,QLCDNumber,QSlider,QVBoxLayout,QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lcd=QLCDNumber(self)#显示组件
sld=QSlider(Qt.Horizontal,self)#滚动条幅组件
vbox=QVBoxLayout()#垂直布局
vbox.addWidget(lcd)#add进来
vbox.addWidget(sld)#add进来
self.setLayout(vbox)#直接进行布局
sld.valueChanged.connect(lcd.display)#将滚动条的valueChanged信号连接到lcd的display插槽。---------------------------
self.setGeometry(300,300,250,150)
self.setWindowTitle('signal&slot')
self.show()
if __name__ == '__main__':
app=QApplication(sys.argv)
ex=Example()
sys.exit(app.exec_())
事件的发送者
# 有时需要知道信号是由哪个控件发出的。对此PyQt5提供了sender()方法。
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn1 = QPushButton("Button 1", self)
btn1.move(30, 50)
btn2 = QPushButton("Button 2", self)
btn2.move(150, 50)
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
self.statusBar()
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Event sender')
self.show()
def buttonClicked(self):#两个按钮连接到了同一个插槽。
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
# =====================================================================发出信号
# 通过QObject创建的对象可以发出信号。下面的示例演示了如何发出自定义信号
import sys
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication
class Communicate(QObject):#这个信号会在按下鼠标时触发,它连接着QMainWindow的close()插槽。
closeApp = pyqtSignal()
class Example1(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 = Example1()
sys.exit(app.exec_())
事件的接收者
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Event handler')
self.show()
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
边栏推荐
- How to judge the performance of static code quality analysis tools? These five factors must be considered
- Common web attacks and defense
- SQL language (6)
- Shell fourth day homework
- 基于MATLAB的常见线性调制方法
- [dynamic planning] 70. Climbing stairs
- ArcMap无法启动解决方法
- Why should the hashcode () method be rewritten when rewriting the equals () method
- Stm32cubemx learning record -- installation, configuration and use
- C# Newtonsoft.Json 高级用法
猜你喜欢

SQL injection LESS18 (header injection + error injection)

Let sports happen naturally, and fire creates a new lifestyle

Small program of vegetable distribution in community

NowCoderTOP1-6——持续更新ing
Definition of information entropy

SQL language (III)

Nowcodertop7-11 - continuous updating

How to judge the performance of static code quality analysis tools? These five factors must be considered

The most efficient note taking method in the world (change your old version of note taking method)

leetcode 剑指 Offer 28. 对称的二叉树
随机推荐
leetcode 剑指 Offer 27. 二叉树的镜像
shell-第六章练习
SQL language (II)
NowCoderTOP1-6——持续更新ing
Loadbalancerlife lifecycle requested by feign client
Several common PCB surface treatment technologies!
同事看了我的代码惊呼:居然是这么在Unity中用单例的
Detailed explanation of zero basis from macro to micro Bert
Some usages of beautifulsoup
城市雕塑典型作品信息管理系统(图片分享系统SSM)
SQL injection LESS18 (header injection + error injection)
Web mobile terminal: touchmove realizes local scrolling
How does the whole network display IP ownership?
Reinforcement learning (IV)
Shell fourth day homework
leetcode 剑指 Offer 28. 对称的二叉树
SQL language (6)
基于cornerstone.js的dicom医学影像查看浏览功能
Esp8266 uses drv8833 drive board to drive N20 motor
Compressed list ziplist of redis