当前位置:网站首页>Qt | 信号和槽的一些总结
Qt | 信号和槽的一些总结
2022-07-28 09:56:00 【InfoQ】
前言:
- 如果同一个信号和同一个槽函数进行了多次连接(connect),是否会造成崩溃?
- 答案是不会,如果同一个信号和同一个槽函数进行多次连接,那么信号在发出后,会多次执行槽函数。
信号和槽详解:
- 声明一个信号需要使用signals关键字,在signals前面不能添加public、private、protected等限定符。信号默认是public的,可以在任何地方进行发射(emit),但是建议只在定义该信号的类及其子类中进行发射改信号。
- 信号只用声明,不需要实现。而且,信号没有返回值,只能是void类型的。
- 只有QObject类及其子类派生的类才可以使用信号和槽机制。使用信号和槽还必须在类声明的最开始处添加Q_OBJECT宏。
- 槽就是普通的C++函数,可以像一般函数一样使用。声明槽要使用slots关键字,一个槽可以是private、public或者protected类型的,也可以被声明为虚函数。
- 槽函数中的参数的类型和信号参数的类型要相对应,且不能比信号的参数多。
connect详解:
- 第一个参数为发射信号的对象。
- 第二个参数为要发射的信号。
- 第三个参数为接收信号的对象。
- 第四个参数为要执行的槽。
- 第五个参数type为关联的方式。
- Qt5之前的形式:connect(dlg, SIGNAL(funcSignal(int)), this, SLOT(funcSlot(int)));
- Qt5加入的一种形式:connect(dlg, &MyDialog::funcSignal, this, &Widget::funcSlot);
信号和槽的自动关联:
- 以这种方式命名的槽函数可以直接和信号关联,不用再使用connect()函数。
- connectSlotsByName()函数是用来支持信号和槽自动关联的,因为setupUi()函数中自动调用了connectSlotsByName()函数,所以要使用自动关联的部件的定义都要放在setupUi()函数调用之前,且必须使用setObjectName()指定它们的objectName,只有这样才能正常使用自动关联。
断开关联 disconnect:
- 断开与一个对象多有信号的所有关联:disconnect(myObject, 0, 0, 0); 等价于myObject->disconnect();
- 断开与一个指定信号的所有关联:disconnect(myObject, SIGNAL(mySignal()), 0, 0);等价于myObject->disconnect(SIGNAL(mySignal()));
- 断开与一个指定的receiver的所有关联:disconnect(myObject, 0, myReceiver, 0); 等价于myObject->disconnect(myReceiver);
- 断开一个指定信号和槽的关联:disconnect(myObject, SIGNAL(mySignal()), myReceiver, SLOT(mySlot())); 等价于myObject->disconnect(SIGNAL(mySignal()), myReceiver, SLOT(mySlot()));还等价于disconnect(myConnection);//myConnection是进行关联时connect()的返回值。
关于信号槽的高级应用:
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)));
然后槽中可以这样写:
switch(i) //i表示是哪个button发生了clicked信号
{
//自定义操作
}边栏推荐
- PHP Basics
- Bit.store, which has attracted much attention, is at a glance of the latest developments
- Which strings will be resolved to null by fastjason?
- JWT 登录认证 + Token 自动续期方案,写得太好了!
- Status Notice ¶
- Winform 生成随机验证码
- 高温持续,公交企业开展安全专项培训
- [openharmony] [rk2206] build openharmony compiler (2)
- 【MySQL】Got an error reading communication packets
- 工业品MRO采购网站有哪些优势?一文带你读懂
猜你喜欢
![[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
Edge team explains how to improve the comprehensive performance experience through disk cache compression technology

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

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

选择供应商服务系统,是大健康产业企业迈向数字化转型的第一步

广州地铁14号线新市墟站开建,白云区居民即将开启双线换乘模式!

2022 uni app parsing token standard - use jsrsasign - climb the pit

Digital construction of pharmaceutical industry is on the verge

OSPF expansion configuration, routing principles, anti ring and re release

ES(8.1)认证题目
随机推荐
CloudCompare&PCL 匹配点采样一致性抑制
医药行业数字化建设,箭在弦上
Flink - checkpoint Failure reason: Not all required tasks are currently running
【MySQL】查询多个ID返回字符串拼接
ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
_HUGE and __IMP__HUGE in “math.h“
Several innovative economic models of platofarm have inspired the current metacosmic market
Basic examples that must be mastered by beginners of C #
LIBCMTD.lib
Array collation commonly used in PHP
选择供应商服务系统,是大健康产业企业迈向数字化转型的第一步
In the era of home health diagnosis, Senzo creates enhanced lateral flow test products
The victory of Dao community, tiger Dao VC wins in governance and consensus
银行入职考试要点汇总
关于软考高级要不要报班学习
Bit.store, which has attracted much attention, is at a glance of the latest developments
Basic knowledge of redis
Experiment 5: user and user group management
JWT 登录认证 + Token 自动续期方案,写得太好了!
[esp32][esp idf] esp32s3 quickly build lvglv7.9