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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark-puls

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值