Qt信号和槽连接方式的选择
来源:互联网 发布:微游戏机 vs 网络盒子 编辑:程序博客网 时间:2024/06/04 23:03
Qt supports these signal-slot connection types
Auto Connection (default) If the signal is emitted in the thread which the receiving object has affinity then the behavior is the same as the Direct Connection. Otherwise, the behavior is the same as the Queued Connection."
Direct Connection The slot is invoked immediately, when the signal is emitted. The slot is executed in the emitter's thread, which is not necessarily the receiver's thread.
Queued Connection The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
Blocking Queued Connection The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.
Note: Using this type to connect objects in the same thread will cause deadlock.
Unique Connection The behavior is the same as the Auto Connection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection is not made and connect() returns false
可以看出一共是5个值:自动、直接、队列、阻塞队列、唯一。
我们先看下 直接和队列。
直接连接的大概意思是:信号一旦发射,槽立即执行,并且槽是在信号发射的线程中执行的。
队列连接的大概意思是:信号发射后当事件循环返回到接收线程时槽函数就执行了,也就是说这种连接方式不是立即触发槽函数的,而是要排队等的,并且是在槽函数的线程中执行。
再来看看 自动和阻塞队列方式
自动连接的大概意思是:信号发射对象如果和槽的执行对象在同一个线程,那么将是直连方式,否则就是队列方式。
阻塞队列方式:在槽函数返回之前槽函数所在的线程都是阻塞的
唯一方式:和直连相同,但是只能一对一连接。
Qt帮助文档中提醒到:Be aware that using direct connections when the sender and receiver live in different threads is unsafe if an event loop is running in the receiver's thread, for the same reason that calling any function on an object living in another thread is unsafe.
即跨线程调QObject对象是不安全的。
Call qRegisterMetaType() to register the data type before you establish the connection。
另外信号槽的参数必须是注册的MetaType,所以当你使用自定义的类型或者 没有注册的类型,都要调用qRegisterMetaType()进行注册,因为Qt需要保存你的参数。
- Qt信号和槽连接方式的选择
- Qt信号和槽连接方式的选择
- Qt信号和槽连接方式的选择
- QT信号槽连接方式
- Qt 信号槽连接方式
- qt 信号连接方式
- qt信号槽之创建和连接自定义的槽
- Qt跨线程信号和槽的连接
- Qt跨线程信号和槽的连接
- qt编程信号和槽连接应注意的问题
- 信号和槽的连接类型 Qt::ConnectionType 解析
- QT自定义Action的信号和槽连接问题
- QT跨线程连接信号和槽
- QT的信号和信号槽
- Qt信号和槽的连接--发射信号而槽未响应
- QT的信号和槽
- Qt的信号和槽
- QT的信号和槽
- 记录一次爬虫经历
- android机型适配终极篇
- EXE注入分析之傀儡进程
- PAT B1048. 数字加密(20)
- 《flask web开发》第11章 博客文章发布表单无法显示的解决方案
- Qt信号和槽连接方式的选择
- JZ2440学习之2440启动方式
- C++11移动语义探讨——从临时对象到右值引用
- nginx 入门、安装与简单使用
- KenDoGrid中的控件为日期时间
- mybatis一对多
- Amateras Modeler
- AngularJS 表单验证
- 倒计时--时分秒