看了下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信号和槽连接方式的选择的更多相关文章

  1. Qt 5 最新信号和槽连接方式以及Lambda表达式

    最近学习Qt,发现新大陆,这里做下记录. 主要内容就是原始Qt4的信号槽连接方式,以及Qt5新版的连接方式,还有件事简单演示一下lambda表达式的使用方式 代码如下 /* * 作者:张建伟 * 时间 ...

  2. QT 信号与槽连接

    转帖 http://www.cnblogs.com/cnhome/archive/2009/10/01/1577277.html 信号(SIGNAL)和槽(SLOT)是Qt编程的一个重要部分.这个机制 ...

  3. Qt信号与槽机制

    一.信号和槽机制 信号和槽用于两个对象之间的通信,信号和槽机制是Qt的核心特征,也是Qt不同于其他开发框架的最突出的特征.在GUI编程中,当改变了一个部件时,总希望其他部件也能了解到该变化.更一般来说 ...

  4. Qt信号与槽使用方法最完整总结

    在图形界面编程中(参考<C++最好的图形库是什么?>),组件之间如何实现通信是核心的技术内容.Qt 使用了信号与槽的机制,非常的高效.简单.易学,方便开发者的使用.本文详细的介绍了Qt 当 ...

  5. Qt信号与槽传递自定义数据类型——两种解决方法

    信号与槽作为qt中的核心机制,在qt应用开发中经常会用的,但是原生的信号与槽连接传参,只支持基本的数据类型,比如char,int, float,double. 如果想要在信号与槽之间传递自定义参数,比 ...

  6. QT 信号与槽connect

    QT 信号与槽connect QT 信号与槽connect connect函数调用几个限制 connect函数代码 QT中信号与槽的连接使用的connect函数是一个静态函数,在类QObject中定义 ...

  7. Qt 信号与槽

    Qt信号与槽的理解 信号和槽机制是 QT 的核心机制,要精通 QT 编程就必须对信号和槽有所了解.信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重 ...

  8. QT信号和槽

    QT信号和槽 ============ 信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性.要正确的处理信号和槽,必须借助一个称为 moc(Meta Object Compiler) ...

  9. 【2017-07-04】Qt信号与槽深入理解之一:信号与槽的连接方式

    今天是个好日子,嗯. 信号槽机制是Qt的特色功能之一,类似于windows中的消息机制,在不同的类对象间传递消息时我们经常使用信号槽机制,然而很多时候都没有去关注connect()函数到底有几种重载的 ...

随机推荐

  1. VR室内定位系统小结

    一.写在开始之前 不管是HTC 的Vive还是OC的CV1,都说明VR 定位设备和手柄都会成为未来VR的发展趋势. VR目前关键就是体验,全身心的投入,身临其境的感觉. 不能总玩着玩着,出戏了.这肯定 ...

  2. Core Location和MapKit的一些简单使用

      Core Location 1. 基本对象是CLLocation,有属性coordinate, altitude, horizontal/vertical Accuracy, timestamp, ...

  3. iOS 数据类型转换

    1.NSString转化为UNICODE String:(NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname c ...

  4. CSDN专栏收集

     Android集 1.Himi李华明的<Android游戏开发专栏>http://blog.csdn.net/column/details/androidgame.html2.老罗的&l ...

  5. 【BZOJ4452】[Cerc2015]Export Estimate 并查集

    [BZOJ4452][Cerc2015]Export Estimate Description 给你一个n个点m条边的无向图,每条边有权值,我们可以选择一个整数lim来生成一个新的图,过程如下:  1 ...

  6. 【BZOJ1594】[Usaco2008 Jan]猜数游戏 二分答案+并查集

    [BZOJ1594][Usaco2008 Jan]猜数游戏 Description 为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力. 游戏开始前,一头指定的奶牛会在 ...

  7. thinkphp---自动验证的问题

    这段时间做一个项目:使用 thinkphp 做了一个自动验证,但是发现如果新增的时候,是能够进行自动验证的,但是在修改的修改的时候,会发现自动验证会失效. 验证的时候,模型是这样写的: protect ...

  8. 从零搭建 vue-cli 脚手架

    前言: 用了几次 vue-cli 做 vue 项目,感觉没什么大问题,虽然也没有用 vue-router 和 vuex .但是心里一直有个梗,就是最初的目录生成和配置文件,一直没动过,也不知道具体原理 ...

  9. python redis基本概念简单操作

    转自:http://www.cnblogs.com/melonjiang/p/5342383.html 一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储 ...

  10. leetcode之Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...