0
点赞
收藏
分享

微信扫一扫

Qt-ConnectionType详解

Qt提供了Qt::ConnectionType类型的枚举来控制信号槽连接的类型,根据connect方法中该类型的值来确定连接类型,其主要区别是决定信号触发的时候槽函数是立即执行还是延迟执行。下面详细讲解这几种类型:

ConstantValueDescription
Qt::AutoConnection0(Default) If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.
Qt::DirectConnection1The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread.
Qt::QueuedConnection2The 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::BlockingQueuedConnection3Same as Qt::QueuedConnection, except that the signalling thread blocks until the slot returns. This connection must not be used if the receiver lives in the signalling thread, or else the application will deadlock.
Qt::UniqueConnection0x80This is a flag that can be combined with any one of the above connection types, using a bitwise OR. When Qt::UniqueConnection is set, QObject::connect() will fail if the connection already exists (i.e. if the same signal is already connected to the same slot for the same pair of objects). This flag was introduced in Qt 4.6.

1、Qt::AutoConnection

如果connect中最后一个参数不填的话那就是默认这个值,那何为Auto呢,这里分了两种情况
如果信号发送的线程和接收者处在同一个线程,那Qt::AutoConnection等价于Qt::DirectConnection,否则Qt::AutoConnection等价于Qt::QueuedConnection。
一般情况下,默认不填我们可以认为是Qt::DirectConnection(直连的方式),除非我们用到了线程,这种情况就是Qt::QueuedConnection。

2、Qt::DirectConnection

顾名思义,看到名称我们就知道,此类型表示信号与槽函数直连的意思,就是信号触发,槽函数立马执行,并且槽函数的执行与信号处在一个线程
这个类型其实也是我们最常用的类型(实际上我们可能也没有自己填写过这个参数),如果不涉及到多线程,基本是采用这种方式。
需要注意的是,这种方式由于信号触发,槽函数立马执行,所以信号被调用处之后的代码需要等待槽函数执行完毕之后才会被调用。

3、Qt::QueuedConnection


Qt训练营内容(一期),更多详细的文章有兴趣的小伙伴可以点击看一看哈,里面有更多优质的内容等着你!

举报

相关推荐

0 条评论