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

4 篇文章 0 订阅


在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

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Qt中,线程通信有以下几种方式: 1. 信号槽机制:在一个线程中,一个对象(信号发射者)通过发射信号,通知其他对象(信号接收者)执行相应的槽函数。这种方式是Qt中最常用的线程通信方式,因为它可以跨线程使用,并且不需要程序员手动管理线程的生命周期。但是,由于信号槽机制是基于事件循环实现的,因此它的实时性可能不够高。 2. 事件机制:在一个线程中,一个对象通过发送事件,通知其他对象执行相应的事件处理函数。这种方式与信号槽机制类似,但是它更加通用,因为事件可以是任何类型的,不仅限于信号。但是,事件机制需要程序员手动管理线程的生命周期,并且实现起来相对复杂。 3. 共享内存:多个线程可以访问同一块共享内存区域,从而实现数据共享。这种方式的优点是效率高,可以实现实时性要求较高的线程通信。但是,由于共享内存存在数据同步的问题,程序员需要手动管理线程的同步和互斥。 4. 消息队列:一个线程可以将消息发送到消息队列中,其他线程可以从消息队列中获取消息进行处理。这种方式的优点是可以实现异步处理,提高程序的性能。但是,消息队列需要程序员手动管理线程的生命周期,并且实现起来相对复杂。 总的来说,Qt提供了多种线程通信的方式,程序员可以根据具体需求选择合适的方式。信号槽机制是最常用的方式,因为它简单易用,并且可以跨线程使用。而共享内存和消息队列则适用于实时性要求较高的线程通信

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间之里

好东西就应该拿出来大家共享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值