0
点赞
收藏
分享

微信扫一扫

【QT】:线程之间的通信操作(主线程与子线程,子线程与子线程和主线程同时通信,附源码案例)

肉肉七七 2022-04-02 阅读 293
c++qt

线程之间的通信操作


在Qt中的线程实现变量的共享等操作常用的主要有全局变量和信号、槽的方式实现,这里主要介绍信号和槽函数的方式实现数据的共享、传递

主线程与子线程之间通过信号、槽实现通信

step1:新建项目工程

step2:槽函数、信号定义

sub_thread1.h子线程文件

#ifndef SUB_THREAD1_H
#define SUB_THREAD1_H

#include <QObject>
#include <QThread>

class sub_thread1 : public QThread
{
    Q_OBJECT
public:
    sub_thread1();


    void run();
signals:
    void test001();

public slots:

};

#endif // SUB_THREAD1_H

sub_thread1.cpp子线程文件

#include "sub_thread1.h"
#include <QDebug>

sub_thread1::sub_thread1()
{

}

void sub_thread1::run()
{
    qDebug("start emit");
    emit test001();
    qDebug("emit end");
}
mainwindow.h  GUI主线程文件

```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "multhread.h"
#include "sub_thread1.h"




QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    sub_thread1 * sub_t;
private:
    Ui::MainWindow *ui;
    

public slots:
    void signalRec();

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sub_t1 = new sub_thread1;
    sub_t1->start();
    connect(sub_t1,&sub_thread1::test001,this,&MainWindow::signalRec);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::signalRec()
{
    qDebug("receive signals");
}

main.cpp文件

#include "mainwindow.h"

#include <QApplication>

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

执行结果:
在这里插入图片描述

子线程与子线程之间通过信号和槽实现通信

step1:新建项目工程

step2:槽函数、信号定义

thread1向thread和maindow同时发送信号

sub_thread.h子线程文件

#ifndef SUB_THREAD_H
#define SUB_THREAD_H

#include <QObject>
#include <QThread>

class sub_thread : public QThread
{
    Q_OBJECT


public:
    sub_thread();
    void run();


signals:

    void subSignalEmit();
public slots:
    void receiveSubThreadSignals();
};

#endif // SUB_THREAD_H

sub_thread.cpp子线程文件

#include "sub_thread.h"

sub_thread::sub_thread()
{

}

void sub_thread::run()
{
    qDebug("sub_thread start run");

}

void sub_thread::receiveSubThreadSignals()
{
    qDebug("received sub_thread1 singels successfully");
}

sub_thread1.h子线程文件

#ifndef SUB_THREAD1_H
#define SUB_THREAD1_H

#include <QObject>
#include <QThread>

class sub_thread1 : public QThread
{
    Q_OBJECT
public:
    sub_thread1();


    void run();
signals:
    void test001();

public slots:

};

#endif // SUB_THREAD1_H

sub_thread1.cpp子线程文件

#include "sub_thread1.h"
#include <QDebug>

sub_thread1::sub_thread1()
{

}

void sub_thread1::run()
{
    qDebug("start emit");
    emit test001();
    qDebug("emit end");
}
mainwindow.h  GUI主线程文件

```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "multhread.h"
#include "sub_thread1.h"
#include "sub_thread.h"



QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    sub_thread * sub_t;
    sub_thread1 * sub_t1;
private:
    Ui::MainWindow *ui;
    

public slots:
    void signalRec();

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sub_t1 = new sub_thread1;
    sub_t1->start();
    sub_t = new sub_thread;
    sub_t->start();
    connect(sub_t1,&sub_thread1::test001,this,&MainWindow::signalRec);
    connect(sub_t1,&sub_thread1::test001,sub_t,&sub_thread::receiveSubThreadSignals);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::signalRec()
{
    qDebug("receive signals");
}
void MainWindow::on_pushButton_clicked()
{
     sub_t->run();
}

main.cpp文件

#include "mainwindow.h"

#include <QApplication>

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

在这里插入图片描述

QT两个子线程之间通过信号-槽通信
https://blog.csdn.net/thequitesunshine007/article/details/105493888

举报

相关推荐

0 条评论