一、深入信号与槽的连接方式
1、深入信号与槽的连接方式
(1)、Qt::DirectConnection : 立即调用
(2)、Qt::QueuedConnection : 异步调用
(3)、Qt::BlockingQueuedConnection : 同步调用
(4)、Qt::AutoConnection : 默认连接
(5)、Qt::UniqueConnection : 单一连接
2、小知识
(1)、信号与槽的连接方式决定槽函数调用时候的相关行为
![](https://i-blog.csdnimg.cn/blog_migrate/aa11f3b7c1d0ef3aa2e38c7dddf2ffe2.png)
3、知识回顾
(1)、每一个线程都有自己的事件队列
(2)、线程通过事件队列接收信号
(3)、信号在事件队列里被处理
![](https://i-blog.csdnimg.cn/blog_migrate/22e4544662c6041c5fd0ed7dff91c7a3.png)
二、各种连接方式详述
MyThread对象的run函数发射对象给MyObject的槽
![](https://i-blog.csdnimg.cn/blog_migrate/f6f33905747a83f361df4ce46520b242.gif)
MyObject.h
![](https://i-blog.csdnimg.cn/blog_migrate/1fe73a2c1623cf07b9af1880b8541394.gif)
MyObject.cpp
![](https://i-blog.csdnimg.cn/blog_migrate/b7477783d615826684541cf95dcf9e6f.gif)
MyThread.h
![](https://i-blog.csdnimg.cn/blog_migrate/f8356d5852f90f6d9d17f6823f435a62.gif)
MyThread.cpp
![](https://i-blog.csdnimg.cn/blog_migrate/8ea8319e09bc5958e0508b951c172596.gif)
main.cpp
1、Qt::DirectConnection : 立即调用
(1)、直接在发送信号的线程中调用槽函数,等价于槽函数的实时调用
![](https://i-blog.csdnimg.cn/blog_migrate/3671defbc324580b12a47ab47f67bd96.png)
![](https://i-blog.csdnimg.cn/blog_migrate/af0276f105438041ff96a5b046eeee6d.gif)
//打印结果
int main(int argc, char *argv[]) tid= 0x1abcvoid MyThread::run() tid= 0x191cvoid MyThread::run() i= 0void MyThread::run() i= 1void MyObject::testSlot() tid= 0x191c//槽函数在发射的线程中被调用,且比void MyThread::run() end先打印,说明是相当于之间调用void MyThread::run() end
![](https://i-blog.csdnimg.cn/blog_migrate/12468d59cc51c4206558483abab15c2e.gif)
2、Qt::QueuedConnection : 异步调用
(1)、信号发送到目标线程(目标线程由对象依赖性决定)的事件队列中,由目标线程处理,当前线程继续往下执行
![](https://i-blog.csdnimg.cn/blog_migrate/46f14f19cb623bf8474143a8936b1e75.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c085162c0645aa72608567ab7207a61d.gif)
//打印结果
int main(int argc, char *argv[]) tid= 0x1aecvoid MyThread::run() tid= 0x1b5cvoid MyThread::run() i= 0void MyThread::run() i= 1void MyThread::run() end
void MyObject::testSlot() tid= 0x1aec//在所依附的线程(主线程中被调用),且比void MyThread::run() end后打印,说明发射信号后线程继续往下执行,为异步调用
![](https://i-blog.csdnimg.cn/blog_migrate/29afc2ddae571a98801e3492444ae158.gif)
3、Qt::BlockingQueuedConnection : 同步调用
(1)、信号发送到目标线程的事件队列中,由目标线程处理;当前线程等待槽函数返回,之后继续往下执行
(2)、注意:目标线程和当前线程必须不同
![](https://i-blog.csdnimg.cn/blog_migrate/44b8f539e77562f8e09959f10972bf7e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cbc639e66b6da07e707793a7c44bcb90.gif)
//打印结果
int main(int argc, char *argv[]) tid= 0x18c4void MyThread::run() tid= 0x1bf8void MyThread::run() i= 0void MyThread::run() i= 1void MyObject::testSlot() tid= 0x18c4//在void MyThread::run() end前打印,说明得等槽函数返回才继续往下执行void MyThread::run() end
![](https://i-blog.csdnimg.cn/blog_migrate/9b295176c4bab81675d218fb34381ff8.gif)
4、Qt::AutoConnection : 默认连接
(1)、AutoConnection 是connect函数第五个参数的默认值,也是工程中最常用的连接方式
![](https://i-blog.csdnimg.cn/blog_migrate/d8c7f576ad835e28fed7741be6c630b8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a7cce336ce8d4a8594b5654984d09fdc.gif)
int main(int argc, char *argv[]) tid= 0x1988void MyThread::run() tid= 0x1970void MyThread::run() i= 0void MyThread::run() i= 1void MyThread::run() end
void MyObject::testSlot() tid= 0x1988
![](https://i-blog.csdnimg.cn/blog_migrate/6b156d592ac8dfb3038ee33660edf500.gif)
5、Qt::UniqueConnection : 单一连接
(1)、描述:
A、功能与AutoConnection相同,自动确定连接类型
B、同一个信号与同一个槽函数只有连接一次
(2)、小知识
A、默认情况下,同一个信号可以多次连接到同一个槽函数
B、多次连接意味着同一个槽函数的多次调用
![](https://i-blog.csdnimg.cn/blog_migrate/f8a99715d4128c4594294a41933b9da8.gif)
int main(int argc, char *argv[]) tid= 0x37cvoid MyThread::run() tid= 0x1b70void MyThread::run() i= 0void MyThread::run() i= 1void MyThread::run() end
void MyObject::testSlot() tid= 0x37c//只调用一次
![](https://i-blog.csdnimg.cn/blog_migrate/d279e0bed76110204527cdc2843cdf93.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/57d578bd4638d2ab6942eeba7abfdd00.png)
三、小结
(1)、信号与槽的连接存在多种方式
(2)、立即调用方式等价于槽函数的实时调用
(3)、默认方式自动确定连接类型
(4)、同步方式中的目标线程与当前线程必须不同
(5)、单一连接方式确保同一个信号与同一个槽函数之间只有一个连接