QT中SIGNAL与SLOT连接的几种方式
来源:互联网 发布:域名申请步骤 编辑:程序博客网 时间:2024/05/24 22:45
signal/slot在底层会使用三种方式传递消息。参见QObject::connect()方法:
bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection )
最后一个参数是就是传递消息的方式了,有四个取值:
Qt::DirectConnection
When emitted, the signal is immediately delivered to the slot.
假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这4个slot按连接的时间顺序调用一遍。显然这种方式不能跨线程(传递消息)。
Qt::QueuedConnection
When emitted, the signal is queued until the event loop is able to deliver it to the slot.
假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这个signal包装成一个 QEvent,放到消息队列里。QApplication::exec()或者线程的
QThread::exec()会从消息队列里取消息,然后调用 signal关联的几个slot。这种方式既可以在线程内传递消息,也可以跨线程传递消息。
Qt::BlockingQueuedConnection
Same as QueuedConnection, except that the current thread blocks until the slot has been delivered. This connection type should only be used for receivers in a different
thread. Note that misuse of this type can lead to dead locks in your application.
与Qt::QueuedConnection类似,但是会阻塞等到关联的slot都被执行。这里出现了阻塞这个词,说明它是专门用来多线程间传递消息的。
Qt::AutoConnection
If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt::DirectConnection; otherwise the signal is queued, as with
Qt::QueuedConnection.
这种连接类型根据signal和slot是否在同一个线程里自动选择Qt::DirectConnection或Qt::QueuedConnection
这样看来,第一种类型的效率肯定比第二种高,毕竟第二种方式需要将消息存储到队列,而且可能会涉及到大对象的复制(考虑sig_produced(BigObject bo),bo需要复制到队
列里)。
- QT中SIGNAL与SLOT连接的几种方式
- Qt的signal/slot的4种连接方式
- QT中signal和slot
- Qt 4自动实现signal和slot的连接
- QT的signal和slot的几种常见使用场景
- Qt的slot和signal
- Qt中Signal与Slot参数问题<一>
- Qt 的 signal与slot底层实现机制详解
- QT中signal和slot的一些注意点保存
- PyQt5学习教程11:几种signal与slot处理事件的方法
- [转载]QT中emit,signal,slot
- QT的signal和slot的理解
- Qt的Signal和Slot机制(一)
- Qt的Signal和Slot机制(二)
- QT中的signal/slot的事件
- Java实现Qt的SIGNAL-SLOT机制
- Qt的signal和slot使用心得
- Qt的线程和 signal-slot
- 7.各种时间函数的使用
- leetcode233. Number of Digit One
- JSP学习笔记之一
- 胡汉三又回来了。。
- ORACLE删除某用户下所有对象
- QT中SIGNAL与SLOT连接的几种方式
- Codeforces Beta Round #95 (Div. 2) -- D. Subway (DFS)
- 一种高效的 vector 四则运算处理方法
- Java职业道路以及技术学习道路
- 找工作
- 个人Linux发行版SwairOS
- 第15章习题解答(二)——《x86汇编语言:从实模式到保护模式》读书笔记41
- PLSQL Developer实现数据库间表结构和数据对比和同步
- 你未必知道的CSS故事:揭开leading的面纱