QT子线程与主线程的信号槽通信

转载: https://blog.csdn.net/jmy5945hh/article/details/34796359

最近用QT做一个服务器,众所周知,QT的主线程必须保持畅通,才能刷新UI。所以,网络通信端采用新开线程的方式。在涉及到使用子线程更新Ui上的控件时遇到了点儿麻烦。网上提供了很多同一线程不同类间采用信号槽通信的方式,但是并不完全适合线程间的信号槽通信,这主要体现在自定义消息的传递上。

 

首先我们看看一般的方式:

 

testthread.h 文件

#ifndef TESTTHREAD_H
#define TESTTHREAD_H
 
#include <QThread>
 
#include "msg.h"
 
class TestThread : public QThread
{
    Q_OBJECT
public:
    explicit TestThread(QObject *parent = 0);
 
protected:
    void run();
 
signals:
    void TestSignal(int);
 
private:
    Msg msg;
};
 
#endif // TESTTHREAD_H


testthread.cpp文件

 

#include "testthread.h"
 
TestThread::TestThread(QObject *parent) :
    QThread(parent)
{
}
 
void TestThread::run()
{
    //触发信号
    emit TestSignal(123);
}

自己定义的类继承了QThread类,重写run函数,然后触发TestSignal信号。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
#include "testthread.h"
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
private slots:
    void DisplayMsg(int);
 
private:
    Ui::MainWindow *ui;
    TestThread *t;
};
 
#endif // MAINWINDOW_H


mainwindow.cpp

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
    //进行connect前必须实例化
    t = new TestThread();   
 
    connect(t, SIGNAL(TestSignal(int)), this, SLOT(DisplayMsg(int)));
 
    //执行子线程
    t->start(); 
}
 
void MainWindow::DisplayMsg(int a)
{
    ui->textBrowser->append(QString::number(a));
}
 
MainWindow::~MainWindow()
{
    delete ui;
}


Mainwindow里面连接信号槽,并且将收到的int参数显示在界面上。

 

运行效果

 

下面我们对程序进行一些简单,修改,使得它传输我们的自定义消息。

 

testthread.h 文件

 

#ifndef TESTTHREAD_H
#define TESTTHREAD_H
 
#include <QThread>
 
#include "msg.h"
 
class TestThread : public QThread
{
    Q_OBJECT
public:
    explicit TestThread(QObject *parent = 0);
    Msg msg;
 
protected:
    void run();
 
signals:
    void TestSignal(Msg);   //Msg!!!
};
 
#endif // TESTTHREAD_H

testthread.h 文件

#include "testthread.h"
 
TestThread::TestThread(QObject *parent) :
    QThread(parent)
{
}
 
void TestThread::run()
{
    msg.int_info = 999;
    msg.str_info = "Hello Main Thread!";
    //触发信号
    emit TestSignal(msg);
}

 

mainwindow.h 文件

 

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
#include "testthread.h"
#include "msg.h"
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
private slots:
    void DisplayMsg(Msg);   //Msg!!!
 
private:
    Ui::MainWindow *ui;
    TestThread *t;
};
 
#endif // MAINWINDOW_H


mainwindow.cpp 文件

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
    //进行connect前必须实例化
    t = new TestThread();
 
    //Msg!!!
    connect(t, SIGNAL(TestSignal(Msg)), this, SLOT(DisplayMsg(Msg)));
 
    //执行子线程
    t->start();
}
 
void MainWindow::DisplayMsg(Msg msg)
{
    ui->textBrowser->append(QString::number(msg.int_info));
    ui->textBrowser->append(msg.str_info);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}

 此时再进行编译,能够通过,但是Qt Creator会有提示

QObject::connect: Cannot queue arguments of type 'Msg'
(Make sure 'Msg' is registered using qRegisterMetaType().)


并且运行程序,不会有任何反应。

 

mainwindow.cpp文件 改动为

 

ui->setupUi(this);
 
qRegisterMetaType<Msg>("Msg");

此时能够正常运行 

 

 

说明:

在线程间使用信号槽进行通信时,需要注意必须使用元数据类型

Qt内生的元数据类型,如int double QString 等

如果要用自己定义的数据类型,需要在connect前将其注册为元数据类型。形式见代码。

 

其他参考: 

https://blog.csdn.net/u014552102/article/details/80288709

https://blog.csdn.net/shell_albert/article/details/44340767

https://blog.csdn.net/apaul001/article/details/38376945

https://blog.csdn.net/shiinayukari/article/details/77679593

https://blog.csdn.net/hulifangjiayou/article/details/50170739

Qt 中,线程线程之间的通信需要注意一些细节。通常情况下,线程中的变量是不能直接被线程访问的。如果需要在线程中访问线程中的变量,可以使用信号机制。 具体做法是,定义一个信号,将线程中的变量作为参数传递给信号,然后在线程中连接该信号,当信号触发时,在函数中获取变量的值。以下是示例代码: ```c++ #include <QCoreApplication> #include <QThread> #include <QDebug> class Worker : public QObject { Q_OBJECT public: void doWork() { // 在线程中发送信号,将线程中的变量作为参数传递 emit signalResult(m_result); } signals: // 定义信号,将线程中的变量作为参数传递 void signalResult(int result); private: int m_result = 0; }; class MyObject : public QObject { Q_OBJECT public: MyObject(QObject* parent = nullptr) : QObject(parent) { // 在线程中连接信号函数 connect(&m_worker, &Worker::signalResult, this, &MyObject::slotResult); } void start() { QThread* thread = new QThread(); m_worker.moveToThread(thread); thread->start(); // 在线程中执行任务 QMetaObject::invokeMethod(&m_worker, "doWork"); } public slots: // 函数,获取信号中传递的变量值 void slotResult(int result) { qDebug() << "Result: " << result; } private: Worker m_worker; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyObject obj; obj.start(); return a.exec(); } ``` 在上述代码中,定义了一个 `Worker` 类,该类中的 `doWork` 函数是在线程中执行的任务,该函数通过信号 `signalResult` 将线程中的变量 `m_result` 作为参数传递给信号。在 `MyObject` 类中,连接了信号函数 `slotResult`,当信号触发时,在函数中获取变量的值。最终输出结果应该是: ``` Result: 0 ``` 需要注意的是,由于 Qt信号机制是线程安全的,因此可以保证在线程中触发信号时,函数会在线程中执行。通过这种方式,就可以在线程中访问线程中的变量了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值