当前位置:网站首页>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 常用的数组整理
- 银行入职考试要点汇总
- 【Gradle】This version of the JMH Gradle plugin requires Gradle 6+, you are using 6.6.
- Today, I want to talk about the data types of MySQL database
- Xiao Hei stands up again and looks at leetcode:653. Sum of two IV - enter BST
- Experiment 5: user and user group management
- LSA and optimization of OSPF
- JWT 登录认证 + Token 自动续期方案,写得太好了!
- ES (8.1) certification topic
- Boss: there are too many systems in the company. Can we realize account interworking?
猜你喜欢

我用小程序容器让移动研发效率提升了5倍!

Arthas tutorial

图解 3 种主流企业架构模式(建议收藏!)
![[openharmony] [rk2206] build openharmony compiler (2)](/img/0c/2e8290403d64ec43d192969f776724.png)
[openharmony] [rk2206] build openharmony compiler (2)
![[esp32][esp idf] esp32s3 quickly build lvglv7.9](/img/39/8efef047d0a9223b97819a54b5edf8.png)
[esp32][esp idf] esp32s3 quickly build lvglv7.9

高温持续,公交企业开展安全专项培训

What are the advantages of MRO purchasing website for industrial products? One article will help you understand

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

Pycharm uses CONDA to call the remote server

Redis interview questions must be known and learned
随机推荐
web之圣杯和双飞翼布局、float、clear、both
OSPF的拓展配置,选路原则,防环及重发布
On July 13, 2021, we collapsed like this
Software testing and quality learning notes 2 - black box testing
广州地铁14号线新市墟站开建,白云区居民即将开启双线换乘模式!
13 probability distributions that must be understood in deep learning
第四步-用户开发环境设置
Install lamp under deepin
[learning notes] border and period
Experiment 5: user and user group management
ES(8.1)认证题目
uni-app进阶之创建组件/原生渲染
[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals
Joint search set
图解 3 种主流企业架构模式(建议收藏!)
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
Experiment 4 uses fdisk to manage hard disk
我用小程序容器让移动研发效率提升了5倍!
It can traverse all files and subfolders under a folder
【MySQL】查询多个ID返回字符串拼接