当前位置:网站首页>Characteristics and usage of QT signal and slot
Characteristics and usage of QT signal and slot
2022-04-22 06:04:00 【*wj】
1. Concept :
The signal ( Signal ) It's an event launched in a particular situation , for example PushButton The most common signal is the mouse
Fired on Impact clicked() The signal .
Slot ( Slot ) Is a function of the response to the signal . Slot is a function , With general C++ The function is the same , Can be defined in any part of the class ( public 、 private or protected ), Can have any parameter , It can also be called directly .
The difference between slot function and general function is : Slot functions can be associated with a signal , When the signal is transmitted , The associated slot function is automatically executed .
Qt Meta object compiler C Meta-0 all ect Compiler, MOC ) It's a preprocessor , Put these before the source program is compiled Qt Feature program is converted to standard C++ Compatible form , Then by the standard C++ The compiler compiles . That's why in classes that use the signal and slot mechanism , You must add a Q_OBJECT The reason for the macro , Only by adding this macro , moc To preprocess the signal and slot code in the class .
About the use of signals and slots , There are some rules to note .
( 1) One signal can be connected to multiple slots :
connect (spinNum, SIGNAL(valueChanged(int)), this , SLOT(addFun(int)) ;
connect (spinNum, SIGNAL(valueChanged (int)), this , SLOT(updateStatus (int));
When the signal and slot function have parameters , stay connect() In the function , Specify the type of parameter , But you can not write the parameter name .
( 2 ) Multiple signals can To connect with A slot :
connect (ui->rBtnBlue , SIGNAL (clicked()) , this, SLOT (setTextFontColor ()));
connect (ui->rBtnRed , SIGNAL (clicked()) , this , SLOT (setTextFontColor ())) ;
connect (ui->rBtnBlack , SIGNAL (clicked()), this , SLOT (setTextFontColor ()));
(3 ) A letter No To connect another Another signal :
connect (spinNum, SIGNAL(valueChanged (int)), this , SIGNAL (refreshinfo(int)) ;
( 4 ) Strictly speaking , The number and type of parameters of signal and slot shall be consistent , At least the parameters of the signal shall not be less than the slot Of ginseng
Count . If it doesn't match , There will be compilation errors or running errors .
( 5 ) In classes that use signals and slots , You must add macros to the class definition QOBJECT.
( 6 ) When a signal is transmitted when , It's related to Slot letter Numbers are usually set The perform , Like a normal call Like a function .
Only when all slot functions associated with the signal are executed , Will execute the code behind the transmitting signal .
class QMyClass : public QObject
{
QOBJECT// Reference signal and slot time , Must quote
public:
// Constructors
Widget(QWidget *parent = nullptr);
// Destructor
~Widget();
private:
//
protected:
//
private slots:
//
}
2. connect() Different parameter forms of functions :
(1) A function prototype in the form of parameters is :
connect(sender , SIGNAL(signal()), , receiver, SLOT (slot())) ;
characteristic : Macros are used here SIGNAL() and l SLOT() Specify signal and slot functions , And if the signal and slot function have parameters , The parameter type shall also be indicated
connect (sender , SIGNAL(valueChanged (int)), this , SLOT(updateStatus (int) );
(2) Another prototype of a function in the form of parameters is :
Applicable to : For signals and slots with default parameters ( That is, the signal name is unique , There are no two signals with different parameters and the same name ) , You can use this form of function pointer for Association , Such as :
connect (lineEdit,&QlineEdit::textChanged ,this, &widget::on_textChanged);
(3) It's easy to get wrong :
For signals with the same name with different parameters, the function pointer cannot be used to correlate the signal with the slot , for example QSpinBox() There are two valueChanged() The signal :
Insert picture description here

版权声明
本文为[*wj]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220541420245.html
边栏推荐
- wgs84坐标转换,地图拾取wgs84坐标工具推荐
- ifix问题汇总Q&A(个人记录)
- CAN 数据帧,远程帧,错误帧,以及出错重连
- stm32 printf 重定向 虚拟示波器
- IWDG
- Golang merges two ordered arrays (written test questions)
- Part 74 leetcode exercise (VII) 7 Integer inversion
- Integer splitting problem (dynamic programming + recursion & record array)
- Blue bridge sprint topic - BFS
- Meilisearch usage record
猜你喜欢
随机推荐
B / S architecture
Integer splitting problem (dynamic programming + recursion & record array)
QT学习之代码颜色区别
What compiler is used for beginners of C language (there is a surprise at the end of the article)
ADC key for learning of Blue Bridge Cup embedded expansion board
Alist easy to use guide
Chapter 87 leetcode sword refers to offer dynamic programming (IV) maximum sum of continuous subarrays
stm32学习笔记5——RGB屏相对位置计算
Standard input, standard output, standard error redirection and pipeline
LeetCode: 剑指 Offer 29. 顺时针打印矩阵.
03-pycharm
第73篇 LeetCode题目练习(六) 6.Z字形变换
CAN 数据帧,远程帧,错误帧,以及出错重连
STM32学习笔记1——最简单的GPIO
IWDG
hp unix上编译openssl并使用
Subsets and problems (backtracking & branch and bound)
基于51单片机和霍尔传感器的测速
11 - process control - for loop
Software test classification








