当前位置:   article > 正文

【QT】:线程之间的通信操作(主线程与子线程,子线程与子线程和主线程同时通信,附源码案例)_qt 子线程发送信号,主线程去处理

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

main.cpp文件

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

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

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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");
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

main.cpp文件

#include "mainwindow.h"

#include <QApplication>

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/465136
推荐阅读
相关标签
  

闽ICP备14008679号