当前位置:网站首页>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
}边栏推荐
- Software testing and quality learning notes 2 - black box testing
- Edge team explains how to improve the comprehensive performance experience through disk cache compression technology
- How to get more marks in the game under the new economic model of Plato farm
- Flink - checkpoint Failure reason: Not all required tasks are currently running
- Being on duty less than 8 hours a day and being dismissed? Tencent's former employees recovered 13million overtime pay, etc., and the court won a compensation of 90000 in the final judgment
- 2021.07.13 我们是这样崩的
- 双指针技巧
- SQL server, MySQL master-slave construction, EF core read-write separation code implementation
- ES (8.1) certification topic
- OSPF的拓展配置,选路原则,防环及重发布
猜你喜欢

软件设计师考前20问,注意啦!!

2021.07.13 我们是这样崩的

Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached

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

博弈论 1.Introduction(组合游戏基本概念、对抗搜索、Bash游戏、Nim游戏)

广州地铁14号线新市墟站开建,白云区居民即将开启双线换乘模式!
![[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals](/img/bf/0a968064a8f7c11b86a2a2820208e6.png)
[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals
![[learning notes] border and period](/img/a4/5493f7eefc7dd0e38bc9a53a92b87b.png)
[learning notes] border and period

Why should I take the first-class constructor examination? How high is the gold content of the first-class Constructor Certificate?

What are the advantages of MRO purchasing website for industrial products? One article will help you understand
随机推荐
JWT 登录认证 + Token 自动续期方案,写得太好了!
Winform 生成随机验证码
Irregular area of OSPF, LSA and serial number
In php7?? And?: Differences between
How to get more marks in the game under the new economic model of Plato farm
ES(8.1)认证题目
13 probability distributions that must be understood in deep learning
Thinking and summary of technical personnel | R & D Efficiency
The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests
Skiasharp's WPF self drawn drag ball (case version)
[openharmony] [rk2206] build openharmony compiler (2)
二维前缀和
Guangzhou metro line 14 xinshixu station is under construction, and residents in Baiyun District are about to start a double line transfer mode!
leetcode——旋转数组的最小数字
Status Notice ¶
[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals
Plato farm - a farm meta universe game with Plato as the goal
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC
Flink - checkpoint Failure reason: Not all required tasks are currently running
matlab特征点提取--记录自用