当前位置:网站首页>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信号
{
//自定义操作
}边栏推荐
- OSPF expansion configuration, routing principles, anti ring and re release
- How to get more marks in the game under the new economic model of Plato farm
- Joint search set
- redis的基础知识
- 【云驻共创】华为云:MetaStudio数字内容生产线,让虚拟世界与现实世界无缝融合
- Install lamp under deepin
- 深度学习必懂的 13 种概率分布
- 医药行业数字化建设,箭在弦上
- Bit.store, which has attracted much attention, is at a glance of the latest developments
- 海量数据TopN问题
猜你喜欢

TCP Basics

关于软考高级要不要报班学习

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

我用小程序容器让移动研发效率提升了5倍!
Edge team explains how to improve the comprehensive performance experience through disk cache compression technology

Redis interview questions must be known and learned

How to get more marks in the game under the new economic model of Plato farm

建筑建材行业B2B电子商务网站方案:赋能建材企业转型升级,实现降本提效

Xiao Hei stands up again and looks at leetcode:653. Sum of two IV - enter BST
![[openharmony] [rk2206] build openharmony compiler (2)](/img/0c/2e8290403d64ec43d192969f776724.png)
[openharmony] [rk2206] build openharmony compiler (2)
随机推荐
银行入职考试要点汇总
JWT login authentication + token automatic renewal scheme, well written!
【FPGA教程案例41】图像案例1——通过verilog读取图片
Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble
【MySQL】查询多个ID返回字符串拼接
OSPF的LSA及优化
Flink - checkpoint Failure reason: Not all required tasks are currently running
SQL server, MySQL master-slave construction, EF core read-write separation code implementation
为报复公司解雇,我更改了项目的所有代码注释!
LinkedList源码按摩,啊舒服
Platofarm has made continuous progress, and has launched the official version and super primitive NFT successively
Xiao Hei stands up again and looks at leetcode:653. Sum of two IV - enter BST
Function introduction and description of @jsontype annotation in fastjson
02.1.2.逻辑类型 bool
10分钟快速入门EVS【玩转华为云】
Tencent technical experts: decrypt the 100 million user products wechat, QQ, King glory... Comprehensively practice on the cloud!
广州地铁14号线新市墟站开建,白云区居民即将开启双线换乘模式!
The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests
Data can't lie. Plato farm is the leader of the meta universe