当前位置:网站首页>Signal and slot mechanism ==pyqt5
Signal and slot mechanism ==pyqt5
2022-07-25 11:39:00 【Brother Dong's cultivation diary】
event Event
be-all GUI Programs are event driven . The event is mainly triggered by the user ,
But there may be other triggers : For example, network connection 、window manager Or timer .
When we call QApplication Of exec_() Method will cause the program to enter the main loop . The main loop gets and distributes Events .
In event model , There are three participants :
- Event source : The event source is the object whose state changes . It generates events . event ( object ) Encapsulates the change of state in the event source
- Event object : The event source object gives the work of event processing to the event receiver .
- Event recipient : The event receiver is the object to be notified
pyqt5 There is a unique signal&slot Mechanism to handle events .
Signal slot is used for communication between objects .signal Triggered when a particular event occurs ,slot It could be anything callable object . When signal When triggered, the... Connected to it will be called slot.
Signal and slot mechanisms
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)# According to the component
sld=QSlider(Qt.Horizontal,self)# Scroll banner components
vbox=QVBoxLayout()# Vertical layout
vbox.addWidget(lcd)#add Come in
vbox.addWidget(sld)#add Come in
self.setLayout(vbox)# Direct layout
sld.valueChanged.connect(lcd.display)# Turn the scroll bar to valueChanged Signal connected to lcd Of display slot .---------------------------
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_())
The sender of the event
# Sometimes you need to know which control is sending the signal . Regarding this PyQt5 Provides sender() Method .
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):# The two buttons are connected to the same slot .
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
# ===================================================================== Signal
# adopt QObject The created object can signal . The following example demonstrates how to emit a custom signal
import sys
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication
class Communicate(QObject):# This signal is triggered when the mouse is pressed , It connects QMainWindow Of close() slot .
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_())
The receiver of the event
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_())
边栏推荐
- [high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
- Learn Luzhi PHP -- tp5.0 uses Chinese as an alias and reports "unsupported data expression"
- Leetcode sword finger offer 28. symmetric binary tree
- Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error
- Detailed explanation of the implementation method of DNS separation and resolution
- 工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
- 黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
- Learn NLP with Transformer (Chapter 1)
- 论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
- Understanding: idea uses Scala to write wordcount programs and generate jar packages
猜你喜欢

新能源销冠宏光MINIEV,有着怎样的产品力?

The most complete detailed tutorial on importing ad into lichuanyuan device packaging Library in history (always white and always cool)

JDBC summary

Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error
![[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training](/img/c8/fe18308ddad5cb2fbccb79d2d8a2b6.png)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training

SQL injection LESS18 (header injection + error injection)

Common linear modulation methods based on MATLAB

教你如何通过MCU配置S2E为TCP Server的工作模式

SQL注入 Less17(报错注入+子查询)

Smart cloud IOT platform STM32 esp8266-01s simple wireless light control
随机推荐
全网显示 IP 归属地,是怎么实现的?
小微企业智能名片管理小程序
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
倍增Floyd「建议收藏」
LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
SQL injection LESS18 (header injection + error injection)
web移动端:touchmove实现局部滚动
相似矩阵,可对角化条件
flinksql client 连接kafka select * from table没有数据报错,如何解决?
tensorflow入门
Activity registration | play with kubernetes container service improvement class officially opened!
SQL language (II)
JS convert pseudo array to array
The most complete detailed tutorial on importing ad into lichuanyuan device packaging Library in history (always white and always cool)
Detailed explanation of lvs-nat and lvs-dr modes of LVS load balancing
[dynamic planning] 70. Climbing stairs
JDBC summary
布局管理==PYQT5
Greedy problem 01_ Activity arrangement problem
Dynamic planning question 05_ Missile interception