Qt 多线程之间的通信 实例 emit

xiaoxiao2021-02-28  34

<div id="article_content clearfix" class="article_content csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post"> <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/htmledit_views-0a60691e80.css"> <div class="htmledit_views">

问题描述:界面线程MainApp为主线程,工作线程MyThread为一子线程,从工作线程向主线程传递位置用于改变主线程中按钮的PushButton位置。

注:Qt的信号与槽机制可以将任何继承自QObject类的对象捆绑在一起,使不同对象之间能够相互通信。

一下是几个实现的文件

// mythread.h

#ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QPushButton> class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QPushButton *pb); ~MyThread(); QPushButton *pb; signals: void change_postion(int); // 用于向主线程发送数据的信号函数, 在.cpp中,没有进行定义,直接使用emit进行数据的发送 protected: void run(); public slots: private: }; #endif // MYTHREAD_H

// mythread.cpp

#include "mythread.h" #include <QDebug> MyThread::MyThread(QPushButton *pb) : QThread() { this->pb = pb; } void MyThread::run() { int i = 0; while(true) { qDebug("The number is : %d", i); i += 10; emit change_postion(i); // 多线程间的通信 将i的值发送到主线程中 this->msleep(1000); if(i == 400) { i = 0; } } } MyThread::~MyThread() { }

// MainWindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtGui/QMainWindow> #include "mythread.h" class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); MyThread *thread; QPushButton *pb; private slots: void move_button(int i); }; #endif // MAINWINDOW_H

// MainWindow.cpp

#include "mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->setGeometry(0, 0, 400, 400); pb = new QPushButton("AA", this); thread = new MyThread(pb); // 将子线程中的信号 与 主线程的反应槽函数进行连接 connect(thread, SIGNAL(change_postion(int)), this, SLOT(move_button(int)), Qt::QueuedConnection); // connect函数的重载,最后一个参数需要注意,不能使用默认 thread->start(); } MainWindow::~MainWindow() { delete thread; } void MainWindow::move_button(int i) { pb->move(i, i); }

// main.cpp

#include <QtGui/QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

实现过程中遇到过的问题:

1.connect函数的第五个参数代表信号与槽的连接模式,线程间的信号与槽不能使用Qt::DirectConnection直接连接方式,因为它要求在发信号的线程内执行槽函数。而Qt::QueuedConnection队列方式将信号转换成事件发送到槽函数所在线程的消息队列中让槽函数所在线程来处理,可以实现线程安全的线程间的通信。这样的时效性也不差,上面的实现中,会在子线程“run()”函数中的第二个Sleep之前执行主线程的“change_position(int)”。

于是在调试的时候子线程的“emit change_position(int);”的下一步并不会立即跳转到主线程的“move_button(int)”,我开始还以为信号中途丢了没送达呢。。而强制使用Qt::DirectConnection模式却怎么也摆脱不了错误。

2.线程间用“信号与槽”传递引用参数的话,一定要加const,因为const文字常量存在常量区中,生命周期与程序一样的长。这样可以避免slot调用的时候参数的运行期已过而使引用无效。

我开始没注意到需要加const,程序运行时并没有报告错误,但是主线程毫无使用子线程传递来的字符串的迹象,这让我更加错误地以为子线程发送的信号丢了。。

转载请注明原文地址: https://www.6miu.com/read-2626964.html

最新回复(0)