QT信号与信号槽之connect

1、connect()函数原型如下所示:

原型1:
static QMetaObject::Connection connect(const QObject *sender, const char *signal,
                        const QObject *receiver, const char *member, Qt::ConnectionType = Qt::AutoConnection);

原型2:
static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal,
                    const QObject *receiver, const QMetaMethod &method,
                    Qt::ConnectionType type = Qt::AutoConnection);

原型3:
inline QMetaObject::Connection connect(const QObject *sender, const char *signal,
                    const char *member, Qt::ConnectionType type = Qt::AutoConnection) const;

2、使用建议如下。

建议所有connect()函数都明确指出最后一个参数, 不要用默认值 Qt::AutoConnection或者在出现与此相关的莫名其妙的问题时,检查该参数。
Qt::DirectConnection 槽函数在emit所在线程执行
Qt::QueuedConnection 槽函数在receiver所在线程执行

3、最后一个参数所有情况分析。

Qt::AutoConnection              0
(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。
如果接收者和信号发出者不在一个线程中,将使用Qt:QueuedConnection。
连接类型在信号发出时确定。


Qt::DirectConnection            1
The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread.
信号发出时槽函数会立即被调用。
槽函数在信号发出者所在线程中执行。


Qt::QueuedConnection            2
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    3
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:QueuedConnection相同,只是信号发出者所在线程会阻塞,直到槽函数返回。
如果接收者和信号发出者在同一线程中应该禁用,否则应用程序可能会死锁。

 

 

你可能感兴趣的:(QT,qt)