Qt——多线程与界面组件的通信

1.示例:在子线程中创建QWidget

TestThread.h

复制代码
#ifndef TESTTHREAD_H
#define TESTTHREAD_H

#include <QObject>
#include <QThread>

class TestThread : public QThread
{
    Q_OBJECT

protected:
    void run();
public:
    explicit TestThread(QObject *parent = nullptr);
};

#endif // TESTTHREAD_H

TestThread.cpp

复制代码
#include "TestThread.h"
#include <QWidget>

TestThread::TestThread(QObject *parent)
    : QThread{parent}
{}
void TestThread::run()
{
    QWidget w;
    w.show();
    exec();
}

Widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget() override;
};
#endif // WIDGET_H

Widget.cpp

复制代码
#include "Widget.h"
#include "TestThread.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    TestThread* ptt = new TestThread();
    ptt->start();
}

Widget::~Widget() = default;

main.cpp

复制代码
#include "Widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return QCoreApplication::exec();
}

运行结果会报错,原因是所有界面组件的操作都只能在主线程中完成,因此,主线程也叫做UI线程

2.子线程如何对界面组件进行更新?

解决方案------信号与槽

  • 在子线程类中定义界面组件的更新信号(updateUI)
  • 在主窗口类中定义更新界面组件的槽函数(setInfo)
  • 使用异步方式连接更新信号到槽函数(updateUI------>setInfo)
    • 子线程通过发射信号的方式更新界面组件
    • 所有的界面组件对象只能依附于主线程

UpdateThread.h

复制代码
#ifndef UPDATETHREAD_H
#define UPDATETHREAD_H

#include <QThread>

class UpdateThread : public QThread
{
    Q_OBJECT

protected:
    void run();
public:
    explicit UpdateThread(QObject *parent = nullptr);

signals:
    void updateUI(QString text);
};

#endif // UPDATETHREAD_H

UpdateThread.cpp

复制代码
#include "UpdateThread.h"

UpdateThread::UpdateThread(QObject *parent)
    : QThread{parent}
{}

void UpdateThread::run()
{
    emit updateUI("begin");

    for(int i=0; i<10; i++)
    {
        emit updateUI(QString::number(i));
        sleep(1);
    }
    emit updateUI("End");
}

Widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "UpdateThread.h"
#include <QPlainTextEdit>

class Widget : public QWidget
{
    Q_OBJECT
    UpdateThread m_thread;
    QPlainTextEdit textEdit;

protected slots:
    void appendText(QString text);

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget() override;
};
#endif // WIDGET_H

Widget.cpp

复制代码
#include "Widget.h"
#include <QObject>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    textEdit.setParent(this);
    textEdit.move(20, 20);
    textEdit.resize(200, 150);
    textEdit.setReadOnly(true);
    connect(&m_thread, SIGNAL(updateUI(QString)), this, SLOT(appendText(QString)));
    m_thread.start();
}
void Widget::appendText(QString text)
{
    textEdit.appendPlainText(text);
}
Widget::~Widget() = default;

main.cpp

复制代码
#include "Widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return QCoreApplication::exec();
}

运行结果:

本质分析:子线程发射信号通知主线程界面更新请求;主线程根据具体信号以及信号参数对界面组件进行修改

3.是否有其他间接的方式可以让子线程更新界面组件的状态------发送自定义事件类

  • 自定义事件类用于描述界面更新细节
  • 在主窗口类中重写事件处理函数event
  • 使用postEvent函数异步发送自定义事件类对象
    • 子线程指定接受消息的对象为主窗口对象
    • 在event事件处理函数更新界面状态

StringEvent.h

复制代码
#ifndef STRINGEVENT_H
#define STRINGEVENT_H

#include <QEvent>
#include <QString>

class StringEvent : public QEvent 
{
    QString m_data;
public:
    const static Type TYPE = static_cast<Type>(QEvent::User + 0xFF);
    explicit StringEvent(QString data = "");
    QString data();
};

#endif // STRINGEVENT_H

StringEvent.cpp

复制代码
#include "StringEvent.h"


StringEvent::StringEvent(QString data) : QEvent(TYPE)
{
    m_data = data;
}
QString StringEvent::data()
{
    return m_data;
}

UpdateThread.h

复制代码
#ifndef UPDATETHREAD_H
#define UPDATETHREAD_H

#include <QThread>

class UpdateThread : public QThread
{
    Q_OBJECT

protected:
    void run();
public:
    explicit UpdateThread(QObject *parent = nullptr);
};

#endif // UPDATETHREAD_H

UpdateThread.cpp

复制代码
#include "UpdateThread.h"
#include <QApplication>
#include "StringEvent.h"

UpdateThread::UpdateThread(QObject *parent)
    : QThread{parent}
{}

void UpdateThread::run()
{
    
    QApplication::postEvent(parent(), new StringEvent("Begin"));
    for(int i=0; i<10; i++)
    {
        QApplication::postEvent(parent(), new StringEvent(QString::number(i)));
        sleep(1);
    }
    QApplication::postEvent(parent(), new StringEvent("End"));
}

main.cpp

复制代码
#include "Widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return QCoreApplication::exec();
}

运行结果:

相关推荐
C^h5 小时前
c++ string
开发语言·c++
raindayinrain5 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的5 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao5 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
学逆向的6 小时前
C 局部静态变量
c语言·开发语言·网络安全
小徐不徐说6 小时前
C++ 接口与插件设计:从工厂注册到 Qt 动态插件
数据库·c++·qt·系统架构·程序设计
大都督会赢的7 小时前
C++第一节课
开发语言·c++
UIU1147 小时前
P1233 [ICPC 2001 Taejon R] 木棍加工
数据结构·c++·算法·动态规划
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
小王C语言7 小时前
Qt常用控件:容器类控件,标题分组框、标签页(浏览器标签)
开发语言·qt