当前位置:网站首页>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
}边栏推荐
猜你喜欢

B2B e-commerce website scheme for building materials industry: enable the transformation and upgrading of building materials enterprises to achieve cost reduction and efficiency improvement

技术人 | 研发效能的思考总结

office2013以上输入数学公式

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

19. 删除链表的倒数第 N 个结点

二分、三分、01分数规划 【第I弹】

In the era of home health diagnosis, Senzo creates enhanced lateral flow test products

定了!就在7月30日!

【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03

二分、三分、01分数规划【第III弹】
随机推荐
Basic examples that must be mastered by beginners of C #
并查集
On July 13, 2021, we collapsed like this
SQL server, MySQL master-slave construction, EF core read-write separation code implementation
Include and require include_ Once and require_ Once difference
Qt | 信号和槽的一些总结
21. 合并两个有序链表
为什么要考一级建造师,一建证书含金量有多高?
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
matlab特征点提取--记录自用
每天在岗不足8小时被辞?腾讯前员工追讨1300万加班费等,法院终审获赔9万
图解 3 种主流企业架构模式(建议收藏!)
【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
Holy Grail of web and double wing layout, float, clear, both
13 probability distributions that must be understood in deep learning
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
Description of landingsite electronic label quppa firmware entering DFU status
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)