当前位置:网站首页>QT | some summaries of signals and slots
QT | some summaries of signals and slots
2022-07-28 10:10:00 【InfoQ】
Preface :
- If the same signal and the same slot function are connected multiple times (connect), Will it cause a crash ?
- answerWill not , If the same signal and the same slot function are connected multiple times , So after the signal is sent , The slot function will be executed multiple times .
Signal and slot details :
- Declaring a signal requires the use of signals keyword , stay signals Cannot add... Before public、private、protected And so on . The signal defaults to public Of , It can be launched anywhere (emit), However, it is suggested that only the class and its subclasses that define the signal should be used to transmit the modified signal .
- Signals can only be declared , There is no need to achieve . and , The signal has no return value , Can only be void Type of .
- Only QObject Class and its subclasses can use the signal and slot mechanism . Using signals and slots must also add... At the beginning of the class declaration Q_OBJECT macro .
- Slots are ordinary C++ function , It can be used like a normal function . Declare that the slot will use slots keyword , A slot can be private、public perhaps protected Type of , It can also be declared as a virtual function .
- The type of the parameter in the slot function should correspond to the type of the signal parameter , And not more than the parameters of the signal .
connect Detailed explanation :
- The first parameter is the object transmitting the signal .
- The second parameter is the signal to be transmitted .
- The third parameter is the object receiving the signal .
- The fourth parameter is the slot to be executed .
- Fifth parameter type For the way of association .
- Qt5 The previous form :connect(dlg, SIGNAL(funcSignal(int)), this, SLOT(funcSlot(int)));
- Qt5 A form of joining :connect(dlg, &MyDialog::funcSignal, this, &Widget::funcSlot);
Automatic correlation of signals and slots :
- Slot functions named in this way can be directly associated with signals , Don't use connect() function .
- connectSlotsByName() Function is used to support automatic correlation between signals and slots , because setupUi() Function is called automatically connectSlotsByName() function , Therefore, the definition of components to be automatically associated should be placed in setupUi() Before function call , And must use setObjectName() Specify their objectName, Only in this way can automatic association be used normally .
Disconnect disconnect:
- Disconnect all associations with multiple signals of an object :disconnect(myObject, 0, 0, 0); Equivalent to myObject->disconnect();
- Disconnect all associations with a specified signal :disconnect(myObject, SIGNAL(mySignal()), 0, 0); Equivalent to myObject->disconnect(SIGNAL(mySignal()));
- Disconnect from a specified receiver All associations of :disconnect(myObject, 0, myReceiver, 0); Equivalent to myObject->disconnect(myReceiver);
- Disconnect a specified signal from the slot :disconnect(myObject, SIGNAL(mySignal()), myReceiver, SLOT(mySlot())); Equivalent to myObject->disconnect(SIGNAL(mySignal()), myReceiver, SLOT(mySlot())); Is equivalent to the disconnect(myConnection);//myConnection Yes, when associating connect() The return value of .
Advanced applications of signal slots :
QSignalMapper *signalMapper = new QSignalMapper(this);
for (i = 0; i < 5; i++)
{
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, i);
}
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(handle(int)));
Then it can be written like this in the slot :
switch(i) //i Which is it button It happened. clicked The signal
{
// Custom actions
}边栏推荐
- Skiasharp's WPF self drawn drag ball (case version)
- It can traverse all files and subfolders under a folder
- JWT 登录认证 + Token 自动续期方案,写得太好了!
- ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
- What are the advantages of MRO purchasing website for industrial products? One article will help you understand
- [ESP32][esp-idf] esp32s3快速搭建LVGLV7.9
- Edge team explains how to improve the comprehensive performance experience through disk cache compression technology
- 技术人 | 研发效能的思考总结
- What are the highlights of B2B2C system? How to help jewelry enterprises build an omni channel multi merchant mall management system
- Introduction to evaluatorfilter
猜你喜欢

13 probability distributions that must be understood in deep learning
![[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号](/img/bf/0a968064a8f7c11b86a2a2820208e6.png)
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号

工业品MRO采购网站有哪些优势?一文带你读懂

定了!就在7月30日!

TCP Basics

Weekly report on July 27, 2022

2021.07.13 我们是这样崩的
![[learning notes] border and period](/img/a4/5493f7eefc7dd0e38bc9a53a92b87b.png)
[learning notes] border and period

Seektiger eco pass STI new progress, log in to ZB on April 14

软件设计师考前20问,注意啦!!
随机推荐
Which strings will be resolved to null by fastjason?
Redis interview questions must be known and learned
Installing MySQL for Linux operating system (centos7)
LSA and optimization of OSPF
leetcode——旋转数组的最小数字
Basic examples that must be mastered by beginners of C #
Introduction to consoleappender
【Gradle】This version of the JMH Gradle plugin requires Gradle 6+, you are using 6.6.
医药行业数字化建设,箭在弦上
Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control
Basic knowledge of redis
【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
Digital construction of pharmaceutical industry is on the verge
【MySQL】查询多个ID返回字符串拼接
【云驻共创】华为云:MetaStudio数字内容生产线,让虚拟世界与现实世界无缝融合
CGAL编译错误
JWT login authentication + token automatic renewal scheme, well written!
What are the highlights of B2B2C system? How to help jewelry enterprises build an omni channel multi merchant mall management system
[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9
B2B2C系统亮点是什么?如何助力珠宝首饰企业打造全渠道多商户商城管理体系