首页 > 其他分享 >QT信号槽

QT信号槽

时间:2023-01-05 13:32:37浏览次数:55  
标签:slot QT thread Connection 线程 信号 Qt


一、连接方式介绍

QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, 
const QObject * receiver, const char *method,
Qt::ConnectionType type = Qt::AutoConnection)
  1. AutoConnection自动连接,默认值。
    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.
    如果信号的发送者和信号的接受者的对象同属一个线程,那个工作方式与直连方式相同;否则工作方式与排队方式相同。
  2. Direct Connection直接连接。
    The slot is invoked immediately, when the signal is emitted. The slot is executed in the emitter’s thread, which is not necessarilythe receiver’s thread.
    当信号发出后,相应的槽函数将立即被调用;emit语句后的代码将在所有槽函数执行完毕后被执行,信号与槽函数关系类似于函数调用,同步执行。
  3. 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.
    内部通过postEvent实现的。当信号发出后,排队到信号队列中,需等到接收对象所属线程的事件循环取得控制权时才取得该信号,调用相应的槽函数。emit语句后的代码将在发出信号后立即被执行,无需等待槽函数执行完毕;此时信号被塞到信号队列里了,信号与槽函数关系类似于消息通信,异步执行。
  4. Blocking Queued Connection阻塞的排队方式。
    The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.Using this type to connect objects in the same thread will cause deadlock.
    内部通过信号量+postEvent实现的。插槽作为队列连接调用,除非当前线程阻塞,直到插槽返回。使用此类型连接同一线程中的对象将导致死锁。
  5. 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. Be aware that using direct connections when the sender and receiver live in different threads is unsafe if an event loop is runningin the receiver’s thread, for the same reason that calling any function on an object living in another thread is unsafe.
    该行为与自动连接相同,如果当前信号和槽已经连接过了,就不再连接了。
    请注意,如果事件循环在接收方的线程中运行,那么当发送方和接收方位于不同线程中时使用直接连接是不安全的,这与调用于另一个线程中的对象上的任何函数都是不安全的道理是一样的。
// Qt4连接方式,信号发送者,信号,信号接受者,处理函数
QObject::connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(qT4_slot()));


// Qt5的新方法,编译会有安全监测
QObject::connect(ui->pushButton,&QPushButton::clicked,this,&Widget::qT5_slot);


// Qt5 Lambda表达式,这里需要注意 Lambda表达式是C++ 11 的内容,
// 所以,需要再Pro项目文件中加入 CONFIG += C++ 11
QObject::connect(ui->pushButton,&QPushButton::clicked,[=](){qDebug()<<"lambda 表达式";});

扩展:
1、​​​Qt原理-窥探信号槽的实现细节​​​

2、Qt信号槽-原理分析
3、​​​Qt高级——Qt信号槽机制源码解析​

4、​​Qt实用技能4-认清信号槽的本质​

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


标签:slot,QT,thread,Connection,线程,信号,Qt
From: https://blog.51cto.com/u_15930680/5990949

相关文章

  • Qt绘图之Paint系统
    Qt不管在屏幕上,还是打印设备上都使用同一套API进行绘画。主要涉及到QPainter,QPaintDevice和QPaintEngine这三个类。QPainter用于执行绘制操作QPaintDevice描述了一个二维空......
  • Qt Meta-Object System
    QtMeta-ObjectSystem一、测试源码widget.h#ifndefWIDGET_H#defineWIDGET_H#include<QWidget>#include<QPainter>#include<QSize>#include<QDebug>classWidget:pu......
  • 压力应变桥信号处理系列隔离放大器差分输入转换0-10mV/0-20mV/0-±10mV/0-±20mV
    ​主要特性DIN11IPO压力应变桥信号处理系列隔离放大器是一种将差分输入信号隔离放大、转换成按比例输出的直流信号导轨安装变送模块。产品广泛应用在电力、远程监控、仪器......
  • 牛客进阶题目5:信号发生器
    这个题目有点离谱,题里什么也没给,需要去题解中才知道方波、锯齿波和三角波最大值都为20,方波周期20,锯齿波周期21,三角波周期40对三种波形具体分析方波:周期为20且最大值也为2......
  • ubuntu下安装mysql + qt编译mysql插件
    sudoapt-getinstallmysql-serverapt-getisntallmysql-clientsudoapt-getinstalllibmysqlclient-dev安装成功后可以看一下有没有进程在监听sudonetstat-tap......
  • Qt线程测试
    Qt线程测试#include"vcmainthread.h"VCMainThread::VCMainThread(QWidget*parent):QMainWindow(parent){ui.setupUi(this);videoThread1=newVi......
  • springboot 连接 mqtt,操作数据库时mqtt断开连接
    主要原因,程序异常导致连接断开第一种,普通类注入bean,为空。学过spring应该知道,这必然为空https://blog.csdn.net/qq_41249513/article/details/108465477https://blog.c......
  • DSP+ZYNQ硬件说明手册【XQTyer】
    【开源资料】XQTyer硬件说明手册.pdf链接:https://share.weiyun.com/7TufhN89密码:2f4mvy    XQ6657Z35/45-EVM(XQTyer评估板)是一款基于TIKeyStone架构C6000......
  • [QT]初识
       昨日はQTを始めて使いました。楽しい感じがしました。MFCより、QTのほどが易しそうです。虽然说,对QT有了初步了解,但是有许多问题还没搞清楚,列个清单  1:何谓交......
  • Qt音视频开发09-ffmpeg内核音视频同步
    一、前言用ffmpeg来做音视频同步,个人认为这个是ffmpeg基础处理中最难的一个,无数人就卡在这里,怎么也不准,本人也是尝试过网上各种demo,基本上都是渣渣,要么仅仅支持极其少量的......