ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Qt一骚操作】Qt:ConnectionType使用讲解【转载】

2021-12-28 16:03:02  阅读:182  来源: 互联网

标签:slot Qt signal ConnectionType QueuedConnection 线程 connect 一骚


原文链接:[Qt]connect()参数Qt:ConnectionType使用讲解_祥的专栏-CSDN博客_qt::connectiontype


文章目录

  • 1.问题来源
  • 2.参数详解
  • 3.使用建议

connect()参数Qt:ConnectionType使用讲解

1.问题来源

一般情况下我们用connect函数不会关注它的最后一个参数,因为它默认是Qt::AutoConnection会自适应,但是有时候还是需要自己指定一下,比较靠谱(最近用到一个多线程之间的通信问题,所以就研究了一下)。

//一般使用,不会关注第5个参数
connect(ui.btn, SIGNAL(clicked()), this, SLOT(slot_openBtn()));

//函数原型,第5个参数默认为 Qt::AutoConnection
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType = Qt::AutoConnection);

2.参数详解

那么我们来关注一下这个枚举类型 Qt::ConnectionType

Constant

Value

Description

解释

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

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类似,但是发送消息后会阻塞,直到等到关联的slot都被执行。[说明它是专门用来多线程间传递消息的,而且是阻塞的]

Qt::UniqueConnection

0x80

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.

这个标志可以和上述标志通过或OR来结合使用。用于失能已经存在的connection。

3.使用建议

那么如何使用呢?

  • 如果是在同一线程里面的操作(signalslot都在同一个线程),那么用Qt::DirectConnection的效率最高(使用默认值Qt::AutoConnection也OK),主要是Qt::DirectConnectionQt::QueuedConnection都需要储存到队列。
  • 如果是多个线程之间进行消息传递(signalslot都在不同线程),那么就要用到Qt::QueuedConnection或者Qt::BlockingQueuedConnection,不过一个是无阻塞的(Qt::QueuedConnection),一个是阻塞的(Qt::BlockingQueuedConnection,发送消息后会阻塞,直到所有的slot都被执行)。

标签:slot,Qt,signal,ConnectionType,QueuedConnection,线程,connect,一骚
来源: https://blog.csdn.net/Monster_H7/article/details/122195182

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有