您的位置:首页 > 编程语言 > Qt开发

对QT 的信号连接类型的理解

2013-04-25 12:56 218 查看
这几天各种纠结于QT信号和槽的机制,对于connect的五个参数的理解,今天有了一点新的认识。

下面先贴出一下QT assistance上的描述

Qt::AutoConnection(default)

If the signal is emitted from a different thread than the receiving object, the signal is queued, behaving as Qt::QueuedConnection. Otherwise, the slot is invoked directly, behaving as Qt::DirectConnection. The type of connection
is determined when the signal is emitted.

Qt::DirectConnection

The slot is invoked immediately, when the signal is emitted.

注意使用这个连接方式的时,它是直接调用,会阻塞直到slot执行完毕,执行环境当然是在调用线程中。

如果在别的线程中使用这种方式的连接来创建窗口这是不允许的,只能在qapplication的事件循环中创建。

Qt::QueuedConnection

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.

而使用这个连接方式的时,它是异步调用,直接返回。而槽函数会在接受者的线程中调用,所以执行环境当然也是在接受者的线程中。这种方式使得在其他线程中创建窗口成为可能。

Qt::BlockingQueuedConnection

Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads.

Note: Violating this rule can cause your application to deadlock.

Qt::UniqueConnection

Same as AutoConnection, 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 will fail.
This connection type was introduced in Qt 4.6。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: