在阅读代码时遇到了有五个参数的connect语句,于是去查找QT Assistant,发现其实他本来就是五个参数,只是平时第五个参数采用缺省参数而不显示。
第五个参数列表:
enum Qt::ConnectionType
Constant | Description |
---|---|
Qt::AutoConnection | (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::DirectConnection | The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread. |
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 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::UniqueConnection | This 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. |
机翻:
常数
价值
描述
Qt: AutoConnection
0
(默认)如果接收器位于发出信号的线程中,则使用Qt::DirectConnection。否则,使用Qt::QueuedConnection。连接类型是在信号发出时确定的。
Qt: DirectConnection
1
当信号发出时,立即调用插槽。槽在信号线程中执行。
Qt: QueuedConnection
2
当控制返回到接收方线程的事件循环时,将调用该槽。插槽在接收方的线程中执行。
Qt: BlockingQueuedConnection
3.
与Qt::QueuedConnection相同,除了信号线程阻塞直到槽返回。如果接收方处于发送信号的线程中,则不能使用此连接,否则应用程序将死锁。
Qt: UniqueConnection
0 x80
这是一个可以使用按位或与上述任何一种连接类型组合的标志。当Qt::UniqueConnection被设置时,如果连接已经存在,QObject::connect()将会失败(也就是说,如果相同的信号已经连接到相同的槽上,对应于相同的对对象)。在Qt 4.6中引入了这个标志。
可以通过connect函数的第五个参数来控制, 信号槽执行时所在的线程
1)自动连接(AutoConnection),默认的连接方式,如果信号与槽,也就是发送者与接受者在同一线程,等同于直接连接;如果发送者与接受者处在不同线程,等同于队列连接。
2)直接连接(DirectConnection),当信号发射时,槽函数立即直接调用。无论槽函数所属对象在哪个线程,槽函数总在发送者所在线程执行,即槽函数和信号发送者在同一线程
3)队列连接(QueuedConnection),当控制权回到接受者所在线程的事件循环时,槽函数被调用。槽函数在接受者所在线程执行,即槽函数与信号接受者在同一线程