Qt跨线程信号和槽的连接(默认方式是直连和队列的折中)

摘要:
Qt支持三种类型的信号-槽连接:1,直接连接,当signal发射时,slot立即调用。此slot在发射signal的那个线程中被执行2,队列连接,当控制权回到对象属于的那个线程的事件循环时,slot被调用。连接类型可能通过以向connect()传递参数来指定。注意的是,当发送者与接收者生存在不同的线程中,而事件循环正运行于接收者的线程中,使用直接连接是不安全的。QObject::connect()本身是线程安全的。

Qt支持三种类型的信号-槽连接:
1,直接连接,当signal发射时,slot立即调用。此slot在发射signal的那个线程中被执行(不一定是接收对象生存的那个线程)
2,队列连接,当控制权回到对象属于的那个线程的事件循环时,slot被调用。此slot在接收对象生存的那个线程中被执行
3,自动连接(缺省),假如信号发射与接收者在同一个线程中,其行为如直接连接,否则,其行为如队列连接。

连接类型可能通过以向connect()传递参数来指定。注意的是,当发送者与接收者生存在不同的线程中,而事件循环正运行于接收者的线程中,使用直接连接是不安全的。同样的道理,调用生存在不同的线程中的对象的函数也是不是安全的。QObject::connect()本身是线程安全的。

This enum describes the types of connection that can be used between signals and slots. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time.

ConstantValueDescription
Qt::AutoConnection0(default) If the signal is emitted from a different thread than the receiving object, the signal is queued, behaving as Qt::QueuedConnection. Otherwise, the slot is invoked directly, behaving as Qt::DirectConnection. The type of connection is determined when the signal is emitted.
Qt::DirectConnection1The slot is invoked immediately, when the signal is emitted.
Qt::QueuedConnection2The 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::BlockingQueuedConnection4Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads.Note:Violating this rule can cause your application to deadlock.
Qt::UniqueConnection0x80Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6.
Qt::AutoCompatConnection3The default type when Qt 3 support is enabled. Same as AutoConnection but will also cause warnings to be output in certain situations. SeeCompatibility Signals and Slotsfor further information.

With queued connections, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. If you try to use a queued connection and get the error message:

 QObject::connect: Cannot queue arguments of type 'MyType'

CallqRegisterMetaType() to register the data type before you establish the connection.

When using signals and slots with multiple threads, seeSignals and Slots Across Threads.

See alsoThread Support in Qt,QObject::connect(), andqRegisterMetaType().

http://blog.csdn.net/seanyxie/article/details/7025183

免责声明:文章转载自《Qt跨线程信号和槽的连接(默认方式是直连和队列的折中)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇概率论中常见分布总结以及python的scipy库使用:两点分布、二项分布、几何分布、泊松分布、均匀分布、指数分布、正态分布js 判断图片是否存在下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Jenkins安装

1、 Jenkins简介1.开源自动化持续集成和部署平台CI、持续集成CD和持续部署2.Jenkins Free风格任务管道Maven项目多配置项目多分支管道任务支持的任务类型,不会执行任何更新;触发器(由Gitlab...

flutter Radio单选框

单选框,允许用户从一组中选择一个选项。...

Selenium操作示例——鼠标悬停显示二级菜单,再点击二级菜单或下拉列表

这两天在python中玩selenium时,我遇到了一个问题,那就是鼠标移动到页面上的一个按钮或菜单,二级菜单或下拉菜单自动弹出,然后二级菜单或者下拉列表自动点击。...

非线性方程(组):MATLAB内置函数 solve, vpasolve, fsolve, fzero, roots [MATLAB]

MATLAB函数求解,vpsolve,fsolve,fzero,根函数和信息概述求解函数多项式型非多项式型一维高维符号数值算法求解支持,获得所有符号解如果解可以签名,当没有符号解时获得根支持符号解方法:利用方程的性质获得标准可解函数的方法基本上是模拟手动操作vpsolve支持,获取所有数值解以获得实根支持$imes$support未知fsolve从初始值获取...

[转]从minio中读取文件流进行下载文件

本文转自:https://blog.csdn.net/ZHANGLIZENG/article/details/82892678一、获取Minio连接publicstaticStringminioUrl;publicstaticStringminioUsername;publicstaticStringminioPassword;@Value("${syste...

Java成长之路

如何学习如何从初级Java程序员成长为合格的架构师,或者一个合格的架构师应该拥有什么样的技术知识体系,这不仅是一个刚进入职场的初级程序员,也是一个工作了三年或五年后感到困惑的老程序员面临的问题。首先必须明确Java的突出之处和不同之处。...