Qt信号和槽连接方式的选择
看了下Qt的帮助文档,发现connect函数最后还有一个缺省参数.
connect函数原型是这样的:
QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection)
最后的ConnectionType是一个缺省的参数,默认为自动连接方式,我们来看一下这个参数有哪些值
Qt supports these signal-slot connection types:
Auto Connection (default) If the signal is emitted in the thread which the receiving object has affinity then the behavior is the same as the Direct Connection. Otherwise, the behavior is the same as the Queued Connection."
Direct Connection The slot is invoked immediately, when the signal is emitted. The slot is executed in the emitter's thread, which is not necessarily the receiver's thread.
Queued Connection 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.
Blocking Queued Connection The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.
Note: Using this type to connect objects in the same thread will cause deadlock.
Unique Connection The behavior is the same as the Auto Connection, 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 is not made and connect() returns false
可以看出一共是5个值:自动、直接、队列、阻塞队列、唯一。
我们先看下 直接和队列。
直接连接的大概意思是:信号一旦发射,槽立即执行,并且槽是在信号发射的线程中执行的。
队列连接的大概意思是:信号发射后当事件循环返回到接收线程时槽函数就执行了,也就是说这种连接方式不是立即触发槽函数的,而是要排队等的,并且是在槽函数的线程中执行。
再来看看 自动和阻塞队列方式
自动连接的大概意思是:信号发射对象如果和槽的执行对象在同一个线程,那么将是直连方式,否则就是队列方式。
阻塞队列方式:在槽函数返回之前槽函数所在的线程都是阻塞的。
唯一方式:和直连相同,但是只能一对一连接。
Qt帮助文档中提醒到:Be aware that using direct connections when the sender and receiver live in different threads is unsafe if an event loop is running in the receiver's thread, for the same reason that calling any function on an object living in another thread is unsafe.
即跨线程调QObject对象是不安全的。
Call qRegisterMetaType() to register the data type before you establish the connection。
另外信号槽的参数必须是注册的MetaType,所以当你使用自定义的类型或者 没有注册的类型,都要调用qRegisterMetaType()进行注册,因为Qt需要保存你的参数。
Qt信号和槽连接方式的选择的更多相关文章
- Qt 5 最新信号和槽连接方式以及Lambda表达式
最近学习Qt,发现新大陆,这里做下记录. 主要内容就是原始Qt4的信号槽连接方式,以及Qt5新版的连接方式,还有件事简单演示一下lambda表达式的使用方式 代码如下 /* * 作者:张建伟 * 时间 ...
- QT 信号与槽连接
转帖 http://www.cnblogs.com/cnhome/archive/2009/10/01/1577277.html 信号(SIGNAL)和槽(SLOT)是Qt编程的一个重要部分.这个机制 ...
- Qt信号与槽机制
一.信号和槽机制 信号和槽用于两个对象之间的通信,信号和槽机制是Qt的核心特征,也是Qt不同于其他开发框架的最突出的特征.在GUI编程中,当改变了一个部件时,总希望其他部件也能了解到该变化.更一般来说 ...
- Qt信号与槽使用方法最完整总结
在图形界面编程中(参考<C++最好的图形库是什么?>),组件之间如何实现通信是核心的技术内容.Qt 使用了信号与槽的机制,非常的高效.简单.易学,方便开发者的使用.本文详细的介绍了Qt 当 ...
- Qt信号与槽传递自定义数据类型——两种解决方法
信号与槽作为qt中的核心机制,在qt应用开发中经常会用的,但是原生的信号与槽连接传参,只支持基本的数据类型,比如char,int, float,double. 如果想要在信号与槽之间传递自定义参数,比 ...
- QT 信号与槽connect
QT 信号与槽connect QT 信号与槽connect connect函数调用几个限制 connect函数代码 QT中信号与槽的连接使用的connect函数是一个静态函数,在类QObject中定义 ...
- Qt 信号与槽
Qt信号与槽的理解 信号和槽机制是 QT 的核心机制,要精通 QT 编程就必须对信号和槽有所了解.信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重 ...
- QT信号和槽
QT信号和槽 ============ 信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性.要正确的处理信号和槽,必须借助一个称为 moc(Meta Object Compiler) ...
- 【2017-07-04】Qt信号与槽深入理解之一:信号与槽的连接方式
今天是个好日子,嗯. 信号槽机制是Qt的特色功能之一,类似于windows中的消息机制,在不同的类对象间传递消息时我们经常使用信号槽机制,然而很多时候都没有去关注connect()函数到底有几种重载的 ...
随机推荐
- 打开Linux ftp服务,如:vsftpd: unrecognized service
打开Linux ftp服务,如:vsftpd: unrecognized service [root@BZXXDBS02 ~]# service vsftpd start vsftpd: unre ...
- IDEA Intellij 打开springboot项目 配置文件无法出现输入提示
需要将java代码和资源文件进行标记
- Runtime 运行时之一:消息传递
什么是Runtime? Runtime顾名思义即为运行时.就是系统运行时候的一些机制,它提供了一些使得对象之间能够传递消息的重要函数,其中最主要的就是消息机制了.相较于C语言而言,C语言使用的是“静态 ...
- filter IE滤镜(Internet Explorer)CSS
收集一些IE滤镜,留作之后开发用. 透明度 #myElement { opacity: .; /* other browsers */ filter: progid: DXImageTransform ...
- LeetCode——Largest Number
Description: Given a list of non negative integers, arrange them such that they form the largest num ...
- SSH电力项目四-显示首页
1.登录页面: 将上一节中的页面放到/WEB-INF/page/目录下,需要登录后才能访问该页面: 对应页面:/WEB-INF/page/menu/home.jsp <%@ page langu ...
- Ant教程
安装ant,去http://ant.apache.org下载 配置环境变量(前提是配置了java环境变量) ANT_HOME G:\Software\ant1.9.7 //ant根目录 在PATH后添 ...
- rest_framework之规范详解 00
接口开发 方式1:缺点:如果有10张表,则需要40个url. urls.py views.py 缺点:如果有10张表,则需要40个url. 接下来就出现了resrful 规范,比较简洁 方式2: ...
- Vue2.0 新手完全填坑攻略——从环境搭建到发布
Jinkey原创感谢 showonne.yubang 技术指导Demo 地址:http://demo.jinkey.io/vue2源码:https://github.com/Jinkeycode/vu ...
- Oracle归档文件夹权限设置错误导致的数据库问题解决
把oracle设置为归档模式并且为归档文件新建文件夹 /home/oracle/app/oracle/arch/orcl 但是在启动或者备份时候经常性出现错误 startup报错 startup同时日 ...