看了下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信号与槽连接的书写规范

    环境 : vs2015 + qt  5.9.9 Qt信号和槽连接失败原因主要有以下几点: 1.槽函数并没有声明在类的public slots(或private slots或protected slot ...

  2. shared_ptr 和 make_shared 的使用(以及qt信号和槽连接)

    std::shared_ptr 是一种智能指针(本身就是一个对象,一个对象,一个对象,不是原始指针,重要的事情说三遍),指向一个对象.它能够记录多少个 shared_ptr 共同指向一个对象,从而消除 ...

  3. Qt信号和槽函数连接不成功原因

    Qt信号和槽连接失败原因主要有以下几点: 1.槽函数并没有声明在类的public slots(或private slots或protected slots)里,因此,所想要成为槽函数的那个函数只是普普 ...

  4. Qt信号与槽的五种连接方式

    qt信号与槽的五种连接方式: 1.默认连接:如果是在同一线程等价于直连,在不同线程等价于队列连接 2.直连:信号在哪,在哪个线程执行(最好只在同一线程中用) 3.队列连接: 槽在哪就在哪个线程执行 ( ...

  5. Qt信号与槽使用方法总结

    前言 在图形界面编程中QT是为首选,组件之间如何实现通信是核心的技术内容.Qt 使用了信号与槽的机制,非常的高效.简单.易学,方便开发者的使用.本文详细的介绍了Qt 当中信号与槽的概念,并演示了各种信 ...

  6. 【Qt】Qt信号与槽使用不当,使程序崩溃

    问题描述 跨线程使用Qt信号和槽,信号发送时间间隔小于槽函数处理时间时,造成程序崩溃. 原因分析 跨线程使用Qt信号和槽时,connect默认是QueuedConnection,队列连接方式. 信号传 ...

  7. QT信号与槽——观察者模式——回调函数

    QT信号与槽--观察者模式--回调函数 1.QT信号与槽机制 1.1信号本质 信号是由于用户对窗口或控件进行了某些操作,导致窗口或控件产生了某个特定事件,这时候 Qt 对应的窗口类会发出某个信号.比如 ...

  8. Qt 信号与槽 讲解与案例

    文章目录 信号与槽 绑定信号与槽 简单案例:按钮的点击 自定义槽函数 自定义信号 信号和槽重载二义性问题 解决方案 案例: 实现两个窗口间的切换 信号与槽 所谓信号槽,实际就是观察者模式(发布-订阅模 ...

  9. Qt信号与槽的拓展和案例

    1.标准信号和槽的使用: 2.自定义的信号与槽的使用: 4. 信号槽拓展 4.1 信号槽使用拓展 一个信号可以连接多个槽函数, 发送一个信号有多个处理动作 需要写多个connect连接 信号的接收者可 ...

最新文章

  1. Velocity(2)——引用
  2. springboot 物联网_物联网架构实例分享
  3. com.android.ddmlib.ShellCommandUnresponsiveException
  4. java中使用什么来做错误处理_JAVA基础--如何通过异常处理错误
  5. Ocelot简易教程(二)之快速开始2
  6. hive 中某个字段等于0_快速了解hive
  7. 信息学奥赛一本通(1241:二分法求函数的零点)
  8. hdu 1247 map
  9. matlab Tricks(二十七)—— 可变输入参数输出参数的适配
  10. nginx的安装及配置文件详解
  11. 51单片机的超声波测距仪制作教程
  12. sublime报错信息乱码_Sublime如何解决中文乱码问题
  13. Clean Architecture 读书笔记
  14. 与六年测试工程师促膝长谈,他分享的这些让我对软件测试工作有了全新的认知~
  15. C# 把word转换成txt
  16. Win10无线网络图标不见了,看这一条就行。
  17. 计算机前沿软件应用课程怎么样,信息技术前沿心得体会|信息技术应用心得体会...
  18. 如何判别lib文件是静态库还是动态库的导入文件呢
  19. 一起学英语第二季第六期
  20. C语言浮点数IEEE754

热门文章

  1. 剑指offer:调整数组顺序使奇数位于偶数前面
  2. CSP认证201512-3 画图[C++题解]:dfs、左下角建系、坐标反着读入
  3. c++输出数据的二进制表示形式
  4. 吴恩达机器学习Ex3作业
  5. html页面如何循环遍历,遍历foreach循环中的HTML DOM元素
  6. Linux MySQL Connector/C++ 编程实例
  7. 台式计算机桌面待机时间怎么调,如何设置电脑屏幕待机时间
  8. python程序设计与科学计算pdf_用Python做科学计算 pdf版
  9. java sleep方法_百战程序员:java线程的休眠和回复
  10. android 循环创建json数组对象,Android-创建JSON数组和JSON对象