当前位置:网站首页>信号与槽机制==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_())
边栏推荐
- The B2B2C multi merchant system has rich functions and is very easy to open!!!
- MySQL | GROUP_ The concat function concatenates the values of a column with commas
- 小微企业智能名片管理小程序
- Why should the hashcode () method be rewritten when rewriting the equals () method
- [动态规划] 70. 爬楼梯
- MLX90640 红外热成像仪测温模块开发笔记(五)
- Loadbalancerlife lifecycle requested by feign client
- B2B2C多商户系统功能丰富,极易二开!!!
- 活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
- Understanding: idea uses Scala to write wordcount programs and generate jar packages
猜你喜欢
随机推荐
A troubleshooting record of DirectShow playback problems
Redis 入门
Small program of vegetable distribution in community
DICOM medical image viewing and browsing function based on cornerstone.js
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
将字符串转换为数字
SQL language (III)
Shell Chapter 5 homework
Filter过滤器解决request请求参数乱码的原理解析
flinksql client 连接kafka select * from table没有数据报错,如何解决?
Fillet big killer, use filter to build fillet and wave effect!
Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
Why should the hashcode () method be rewritten when rewriting the equals () method
Definition of information entropy
SQL language (4)
数据库完整性——六大约束学习
Learn Luzhi PHP -- tp5.0 uses Chinese as an alias and reports "unsupported data expression"
My colleague looked at my code and exclaimed: how can I use a singleton in unity
Several common PCB surface treatment technologies!
[递归] 938. 二叉搜索树的范围和









