当前位置:网站首页>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 !!
边栏推荐
- tensorflow求解泊松方程
- 华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
- P.A.R.A 方法在思源的简易应用(亲测好用)
- The role of the symbol @ in MySQL
- Leetcode daily practice (longest substring without repeated characters)
- 【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
- Vulnerability recurrence ----- 34. Yapi remote command execution vulnerability
- IDE Eval reset unlimited trial reset
- 3.4 fixed number of cycles II
- The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
猜你喜欢

3.3 one of the fixed number of cycles

ICML 2022 ぷ the latest fedformer of the Dharma Institute of Afghanistan ⻓ surpasses SOTA in the whole process of time series prediction

Mobile terminal click penetration

About tensorflow using GPU acceleration

Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le

等保三级密码复杂度是多少?多久更换一次?

2022年中国音频市场年度综合分析

【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)

继手机之后 报道称三星也削减了电视等家电产品线的产量

Weekly snapshot of substrate technology 20220411
随机推荐
C language course design
面试半年,上个月成功拿到阿里P7offer,全靠我啃烂了这份2020最新面试题!
事件监听机制
What is the level 3 password complexity of ISO? How often is it replaced?
基于 Nebula Graph 构建百亿关系知识图谱实践
是不是只要支持JDBC / ODBC协议的客户端恐惧,PolarDB-X可通过相关工具的客户端访问?
Bit. Store: long bear market, stable stacking products may become the main theme
Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration
Hierarchical clustering and case analysis
Sigkdd22 | graph generalization framework of graph neural network under the paradigm of "pre training, prompting and fine tuning"
Raspberry pie preliminary use
带你认识图数据库性能和场景测试利器LDBC SNB
阿里云刘珅孜:云游戏带来的启发——端上创新
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
ICML 2022 ぷ the latest fedformer of the Dharma Institute of Afghanistan ⻓ surpasses SOTA in the whole process of time series prediction
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
2022年中国音频市场年度综合分析
A distribution fission activity is more than just a circle of friends!
深耕数字化,引领云原生,服务更多开发者