当前位置:网站首页>Qt5 signal and slot mechanism (basic introduction to signal and slot)
Qt5 signal and slot mechanism (basic introduction to signal and slot)
2022-06-27 16:23:00 【Hua Weiyun】
QT5 Signal and slot mechanism
1.1 What is? Signals and slots
Signal slot mechanism and Windows The next message mechanism is similar to , The message mechanism is based on callback functions ,Qt Replace function pointers with signals and slots in , Make the program safer and simpler .
Signals and slots are used for communication between objects , The signal / The slot mechanism is Qt A core feature of .
In graphical user interface programming , We often notify one widget of a change to another via the signal slot .
• The signal
When an object changes its state , The signal is transmitted by the object (emit) get out , And the object is only responsible for sending signals , It doesn't know who's receiving the signal at the other end . In this way, the real information encapsulation is achieved , Ensure that the object is used as a real software component .
• Slot
For receiving signals , And slots are just ordinary object member functions . A slot doesn't know if there is any signal connected to itself . And the object doesn't know the specific communication mechanism .
• Signal to slot connection
All from QObject Or its subclass ( for example Qwidget ) Derived classes can contain both signals and slots . Because the signal is connected to the slot through QObject Of connect() Member functions .
connect(sender, SIGNAL(signal), receiver, SLOT(slot));
among sender And receiver Is a pointer to an object ,SIGNAL() And SLOT() Is a macro that converts signals to slots .
• characteristic
One signal can be connected to multiple slots
When the signal is transmitted , Will An indefinite sequence Call the slots one by one .
Multiple signals can be connected to the same slot
That is, no matter which signal is transmitted , Will call this slot .
Signals can be directly connected to each other
When transmitting the first signal , You can also send a second signal .
• What should be paid attention to
1. The signal and slot mechanism is the same as the call of ordinary functions , If not used properly , It is also possible to generate dead loops during program execution . therefore , When defining a slot function, be sure to avoid indirectly forming an infinite loop , That is, the same received signal is transmitted again in the slot . for example , In the example given above, if mySlot() Add a statement to the slot function emit mySignal() An endless cycle can be formed .
2. If a signal is associated with multiple slots , that , When this signal is transmitted , The order in which the associated slots are activated will be random .
3. Macro definitions cannot be used in signal and slot The parameters of the .
4. The number and type of parameters of signal and slot must be consistent .
1.2 Signal and slot sample program
1.2.1 Manual Association
1. The click signal of the button , Associated to the corresponding slot function .
connect(ui->pushButton_delete_server,SIGNAL(clicked()),this,SLOT(slot_delete_Server())); |
Argument parsing :
ui->pushButton_delete_server : Signal source object
SIGNAL(clicked()): A signal from a signal source
This: The object at the receiving end
SLOT(slot_delete_Server()): Slot function at the receiving end
1.2.2 Automatic association
.png)
1.3 Custom signal slot
1.3.1 Define your own signals and slot functions
Custom signal and slot functions need to be defined in the class body class !
Sample code :
/* Inherit and TCP The server */ classtcp_server:publicQTcpServer { publicslots:// Custom slot function voidtcp_server_slot_RXdata(QStringstr,intlen); signals: /* Declare custom signals */ voidTcp_Server_Signal(QStringstr,intlen);// Display the received data in the text box }; |
Be careful : Signals and slot functions can have new parameters , But there can't be a return value !
1.3.2 Send custom signal
Sample code :
voidtcp_server::tcp_server_slot_RXdata(QStringstr,intlen) { emitTcp_Server_Signal(str,len);// Sending signal } |
emit Keywords are used to send signals !
grammar :
emit The signal ;
1.4 Signal communication between two classes
If we want to achieve B Such access A Class , The method of defining the signal and slot !
step :
1. stay A Class defines a slot function .
2. stay B Class defines a signal .
3. stay A Class to associate signals with slots !
connect(B Object pointer to class ,SIGNAL(B Class signal )),A Object pointer to class ,SLOT(A Class )); |
4. stay B Class member functions send defined signals
emit Signal function ; |
5. B Class sends a signal ,A The slot function of class will get the corresponding !!
边栏推荐
- 守护雪山之王:这些AI研究者找到了技术的新「用武之地」
- NFT双币质押流动性挖矿dapp合约定制
- What is the open source compatibility of the current version of polardb-x? mysql8?
- # Cesium实现卫星在轨绕行
- Leetcode daily practice (sum of two numbers)
- Taishan Office Technology Lecture: the first difficulty is vertical positioning
- Four characteristics of transactions
- About how vs2019c # establishes the login interface, the user name and password entered must match the records in the access database
- C语言集合运算
- National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
猜你喜欢

ICML 2022 ぷ the latest fedformer of the Dharma Institute of Afghanistan ⻓ surpasses SOTA in the whole process of time series prediction
![[pygame Games] ce jeu](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)

Li Chuang EDA learning notes 16: array copy and array distribution

LeetCode每日一练(杨辉三角)
#yyds干货盘点#简述chromeV8引擎垃圾回收

Etcd可视化工具:Kstone部署(一),基于Helm快速部署

Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe

Source NAT address translation and server mapping web page configuration of firewall Foundation
随机推荐
华为云DevCloud重磅发布四大新能力,创下国内两项第一
Jialichuang EDA professional edition all offline client release
实现简单的三D立方体自动旋转
Bit. Store: long bear market, stable stacking products may become the main theme
一场分销裂变活动,不止是发发朋友圈这么简单!
LeetCode每日一练(无重复字符的最长子串)
目前PolarDB-X是不支持数据库自制服务DAS么?
米哈游起诉五矿信托,后者曾被曝产品暴雷
LeetCode每日一练(杨辉三角)
Li Chuang EDA learning notes 16: array copy and array distribution
Open source 23 things shardingsphere and database mesh have to say
特殊函数计算器
The role of the symbol @ in MySQL
QT audio playback upgrade (7)
16 -- remove invalid parentheses
tensorflow求解泊松方程
Does polardb-x open source support mysql5.7?
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
C语言教师工作量管理系统
E modulenotfounderror: no module named 'psychopg2' (resolved)