当前位置:网站首页>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
}边栏推荐
- Today, I want to talk about the data types of MySQL database
- 定了!就在7月30日!
- MySQL 为什么有时候会选错索引?
- The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests
- Digital construction of pharmaceutical industry is on the verge
- 谈谈基于JS实现阻止别人调试通过控制台调试网站的问题
- In retaliation for the dismissal of the company, I changed all code comments of the project!
- [openharmony] [rk2206] build openharmony compiler (2)
- 软件设计师考前20问,注意啦!!
- [jzof] 14 cut rope
猜你喜欢

巧用ngx_lua做流量分组

API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
![[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号](/img/bf/0a968064a8f7c11b86a2a2820208e6.png)
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号

Basic examples that must be mastered by beginners of C #

OSPF的LSA及优化

【JZOF】14剪绳子

The high temperature continues, and public transport enterprises carry out special safety training

The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests
![[jzof] 14 cut rope](/img/36/6f58b443a549ad245c1c4cfe5d13af.png)
[jzof] 14 cut rope

OSPF的拓展配置,选路原则,防环及重发布
随机推荐
Function introduction and description of @jsontype annotation in fastjson
Status Notice ¶
office2013以上输入数学公式
【MySQL】Got an error reading communication packets
On July 13, 2021, we collapsed like this
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
为报复公司解雇,我更改了项目的所有代码注释!
13 probability distributions that must be understood in deep learning
7.27 minimum spanning tree phased test problem solution
Holy Grail of web and double wing layout, float, clear, both
web之圣杯和双飞翼布局、float、clear、both
ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
leetcode——旋转数组的最小数字
Introduction to evaluatorfilter
【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
PHP Basics
Tencent technical experts: decrypt the 100 million user products wechat, QQ, King glory... Comprehensively practice on the cloud!
Flink - checkpoint Failure reason: Not all required tasks are currently running
Introduction to timebasedrollingpolicy
双指针技巧