首页 > 其他分享 >[Qt]connect()参数Qt:ConnectionType使用讲解

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

时间:2023-05-16 14:36:38浏览次数:36  
标签:slot Qt signal ConnectionType QueuedConnection 线程 connect

https://blog.csdn.net/humanking7/article/details/86064859

 

创文章,欢迎转载。转载请注明:转载自 祥的博客

原文链接:https://blog.csdn.net/humanking7/article/details/86064859

文章目录
@[toc]
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);
1
2
3
4
5
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.使用建议
那么如何使用呢?

如果是在同一线程里面的操作(signal和slot都在同一个线程),那么用Qt::DirectConnection的效率最高(使用默认值Qt::AutoConnection也OK),主要是Qt::DirectConnection和Qt::QueuedConnection都需要储存到队列。
如果是多个线程之间进行消息传递(signal和slot都在不同线程),那么就要用到Qt::QueuedConnection或者Qt::BlockingQueuedConnection,不过一个是无阻塞的(Qt::QueuedConnection),一个是阻塞的(Qt::BlockingQueuedConnection,发送消息后会阻塞,直到所有的slot都被执行)。
————————————————
版权声明:本文为CSDN博主「祥知道」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/humanking7/article/details/86064859

标签:slot,Qt,signal,ConnectionType,QueuedConnection,线程,connect
From: https://www.cnblogs.com/chinasoft/p/17405522.html

相关文章

  • Secure Connection Failed 安全连接失败无效的证书解决
    SecureConnectionFailed安全连接失败无效的证书解决删除配置文件即可 点击菜单帮助 选择更多故障排除信息 找到配置文件 把浏览器关闭打开配置删除 cert9.db即可   添加例外即可进入 ......
  • Qt ffmpeg yolov5 tensorrt 高性能部署,使用tensorrt推理yolov5模型,封装成了dll, 支
    Qtffmpegyolov5tensorrt高性能部署,使用tensorrt推理yolov5模型,封装成了dll,支持多窗口多线程推理,本项目为4窗口版,各个窗口支持识别类别,阈值,roi区域等设置。算法支持onnxruntime,tensorrt推理,以及推理加deepsort,bytetrack和kcf多目标跟踪。ID:353200676908443403......
  • tensorrt yolov5 QT 智能监控平台。 yolov5使用 tens
    tensorrtyolov5QT智能监控平台。yolov5使用tensorrt推理封装成dll,支持多线程多任务,可同时并行加载不同模型,同时检测。Qt开发的监控平台,支持不同平台部署,视频监控,录像回放,电子地图,日志和系统设置应有尽有。视觉监控,同时加载16路视频,同时并行检测任务,可网络流可本地视频。ID:316......
  • Python_报错:curl: (7) Failed to connect to raw.githubusercontent.com port 443: Op
    解决:https://blog.csdn.net/Jimmmyking/article/details/126105788作为mac的用户,如果你还没安装Homebrew那真的就太遗憾了,应为其真的很好用,然后安装Homebrew有时候有不是那么简单,会出现很多奇奇怪怪的错误,如下是我本人第一次安装就成功,其重要用的是中科大的brew主体,使用这个只需......
  • Qt中文乱码
      //直接设置中文,会出现乱码ui.pushButton->setText("中文");//方法一ui.pushButton->setText(QString::fromUtf16(u"中文"));//方法二ui.pushButton->setText(QString::fromLocal8Bit("中文")); 测试过,在main函数中使用QTextCodec方法,无效。还是上面的方法简......
  • qtime 的实现
    QTime、QDate、QDateTimeQDate使用一个uint的变量来记录从儒略日到指定日期的天数长度QDate中只有一个私有变量:uintjd,jd用来标识儒略日(公元前4713年1月1日中文12点依赖的天数)以来的天数QDate的两个主要函数是指定年月日返回jd数据,以及根据jd计算年月日staticuint......
  • Oracle中start with...connect by prior子句用法
    connectby是结构化查询中用到的,其基本语法是:select...fromtablenamestartwith条件1connectby条件2where条件3;例:select*fromtablestartwithorg_id='HBHqfWGWPy'connectbypriororg_id=parent_id;简单说来是将一个树状......
  • Qt信号槽
    Qt信号槽Qt的信号槽,除了使用信号连接槽,还可以信号连接信号,断开信号,发送(转发)信号。一个信号可以连接一个槽,也可以一个信号连接多个槽,还可以多个信号连接一个槽。 //信号连接槽connect(发送者,信号,接受者,槽);//信号连接信号connect(发送者,信号,接受者,信号);......
  • 使用4G通信模块和MQTT协议,完成物联网设备开发。
    使用4G通信模块和MQTT协议,完成物联网设备开发。(1)安装并使用4G模块通信模块,建立microPython开发环境;(2)使用提供的Demo开发例程,使用MQTT传输协议连接阿里或腾讯网站,完成物联网设备开发。(3)将温湿度信息上传到网站;(4)手机APP查看数一、这是我之前写关于阿里云怎么在线调试设备的。......
  • qt5.14.modbus rtu源码,运行无问题! ---Modbus具有两种串行
    qt5.14.modbusrtu源码,运行无问题!---Modbus具有两种串行传输模式:分别为ASCII和RTU。此源代码是RTU。Modbus是一种单主站的主从通信模式,Modbus网络上只能有一个主站存在,主站在Modbus网络上没有地址,每个从站必须有唯一的地址,从站的地址范围为0-247,其中0为广播地址,从站的实际地址范......