【QT】QT::QueuedConnection Connect语句的五个参数

在阅读代码时遇到了有五个参数的connect语句,于是去查找QT Assistant,发现其实他本来就是五个参数,只是平时第五个参数采用缺省参数而不显示。
第五个参数列表:
enum Qt::ConnectionType

ConstantDescription
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::DirectConnectionThe slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread.
Qt::QueuedConnectionThe 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::BlockingQueuedConnectionSame 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::UniqueConnectionThis 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),当控制权回到接受者所在线程的事件循环时,槽函数被调用。槽函数在接受者所在线程执行,即槽函数与信号接受者在同一线程

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
`Qt::QueuedConnection` 是 Qt 中信号和槽的一种连接方式,用于在不同线程之间进行连接,实现线程安全的信号和槽通信。 在使用 `Qt::QueuedConnection` 连接信号和槽时,当信号被触发时,槽函数不会立即执行,而是将槽函数的执行放入事件循环中,等待下一次事件循环执行。这样可以保证槽函数的执行是在接收信号对象所在的线程中完成的,避免了线程安全问题。 下面是一个使用 `Qt::QueuedConnection` 连接信号和槽的例子: ``` class Worker : public QObject { Q_OBJECT public: Worker() {} signals: void valueChanged(int newValue); public slots: void doWork() { int value = 0; while (value < 100) { ++value; emit valueChanged(value); QThread::msleep(100); } } }; class Widget : public QWidget { Q_OBJECT public: Widget() { m_worker = new Worker(); m_thread = new QThread(); m_worker->moveToThread(m_thread); connect(m_worker, &Worker::valueChanged, this, &Widget::updateValue, Qt::QueuedConnection); connect(m_thread, &QThread::started, m_worker, &Worker::doWork); m_thread->start(); } ~Widget() { m_thread->quit(); m_thread->wait(); delete m_worker; delete m_thread; } private slots: void updateValue(int value) { m_label->setText(QString::number(value)); } private: QLabel *m_label; Worker *m_worker; QThread *m_thread; }; ``` 在这个例子中,我们创建了一个 Worker 类,它有一个 valueChanged 信号和一个 doWork 槽函数。在 doWork 函数中,它会每隔 100ms 发出一个 valueChanged 信号,将自增的值传递给信号的接收者。 在 Widget 类中,我们创建了一个 Worker 对象,并将它移动到一个新的线程中。通过 `Qt::QueuedConnection` 连接 Worker 的 valueChanged 信号和 Widget 的 updateValue 槽函数。当信号被触发时,槽函数的执行会被放入事件循环中,等待下一次事件循环执行。这样,updateValue 槽函数的执行就会在 Widget 所在的主线程中完成,避免了线程安全问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值