我对Qt信号阻塞的理解是:
通过信号槽机制的方式调用函数
应用场景为多线程下子线程主动告知主线程某些信息并等待主线程处理完信息
下面来看代码实现:
mythread.h/.cpp
1 //mythread.h : 2 class MyThread : public QThread 3 { 4 Q_OBJECT 5 public: 6 MyThread(); 7 ~MyThread(); 8 9 signals: 10 void slgTest(int iCount); 11 12 private: 13 QTimer* _pTimer; 14 void run(); 15 16 private slots: 17 void sltTimer(); 18 }; 19 20 //mythread.cpp 21 MyThread::MyThread(): 22 _pTimer(nullptr) 23 { 24 } 25 26 MyThread::~MyThread() 27 { 28 if(nullptr != _pTimer){ 29 _pTimer->stop(); 30 delete _pTimer; 31 } 32 this->quit(); 33 this->wait(); 34 } 35 36 void MyThread::run() 37 { 38 _pTimer = new QTimer; 39 connect(_pTimer,&QTimer::timeout,this,&MyThread::sltTimer,Qt::DirectConnection); 40 _pTimer->start(500); 41 this->exec(); 42 } 43 44 void MyThread::sltTimer() 45 { 46 static int s_iCount = 0; 47 s_iCount++; 48 qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<" begin"; 49 emit slgTest(s_iCount); 50 qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<" end"; 51 }
mainwindow.h/.cpp
1 //mainwindow.h 2 class mainWindow : public QWidget 3 { 4 Q_OBJECT 5 6 public: 7 explicit mainWindow(QWidget *parent = 0); 8 ~mainWindow(); 9 10 private: 11 Ui::mainWindow *ui; 12 MyThread _myThread; 13 14 private slots: 15 void sltReveice(int iCount); 16 }; 17 18 //mainwindow.cpp 19 mainWindow::mainWindow(QWidget *parent) : 20 QWidget(parent), 21 ui(new Ui::mainWindow) 22 { 23 ui->setupUi(this); 24 connect(&_myThread,SIGNAL(slgTest(int)),this,SLOT(sltReveice(int))); 25 _myThread.start(); 26 } 27 28 mainWindow::~mainWindow() 29 { 30 delete ui; 31 } 32 33 34 void mainWindow::sltReveice(int iCount) 35 { 36 qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<" begin"; 37 QTime current_time =QTime::currentTime(); 38 qDebug()<<"time:"<<"minute:"<<current_time.minute()<<"second:"<<current_time.second()<<"msec:"<<current_time.msec(); 39 qDebug()<<"received data:"<<iCount; 40 qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<" end"; 41 }
PS:此处代码记录一笔
1、QTimer的构造一定要在run()函数内部才能实现定时器触发的槽函数在线程中构造,然后进行消息循环。
2、信号槽链连接一定要写成:connect(_pTimer,&QTimer::timeout,this,&MyThread::sltTimer);
如果写成connect(_pTimer,SIGNAL(*),this,SLOT(*));形式会提示链接不到槽函数,原因应该是run函数是重写父类QThread的,run里的this指向的应该是QThread,而不是mythread。
3、线程中的定时器信号槽链接参数一定要是Qt::DirectConnection(信号槽连接参数可以参考这个博客:https://www.cnblogs.com/xupeidong/p/9487494.html)
4、一定要有Q_OBJECT,宏定义Q_OBJECT是任何实现信号、槽或属性的强制性要求。
上面的代码运行结果:
如上图所示,两个线程的数据交互没有什么问题,但是如果我在主线程中sleep一下呢(模拟耗时函数)
在sltReveice()中添加usleep(1000);(linux下sleep头文件:#include <unistd.h>)
运行结果:
如上图所示开始严重不同步了
PS:此处会不断触发,信号会不断排队(应该仅在多线程的应用背景下有这种排队的情况)
现在设置为阻塞connect(&_myThread,SIGNAL(slgTest(int)),this,SLOT(sltReveice(int)),Qt::BlockingQueuedConnection);
运行结果:
如上图所示现在整齐排列了,子线程在等待主线程处理完成,回到开始说的“通过信号槽机制的方式调用函数”,也就是说相当于子线程调用了一个函数。
题外:上面提到了线程发送的信号不断排队的情况。
程序在上面程序中稍作改动
1、为mythread添加私有变量并在每次子线程定时器到的时候自增
2、mythread的私有变量在达到20的时候关闭定时器
3、为mythread添加获取私有变量值的方法
此处不再展示代码
代码运行结果为:
如上图所示定时器已经结束,但是主线程的槽函数依然在被触发
此处处理方式是进入槽函数就断开连接,退出函数时再连接(不再展示程序代码和运行结果)
断开连接函数:disconnect
PS断开连接的方式:
1、大量断开信号与槽函数
断开所有连接到某一对象的信号槽:disconnect(myObject, 0, 0, 0); myObject->disconnect();
断开所有连接到某一信号的信号槽:disconnect(myObject, SIGNAL(mySignal()), 0, 0); myObject->disconnect(SIGNAL(mySignal()));
断开两个对象之间的所有信号槽关系:disconnect(myObject, 0, myReceiver, 0);myObject->disconnect(myReceiver);
2、指定信号简洁断开
QMetaObject::Connection dis = connect(....); disconnect(dis);
sender()->disconnect();