一个QT多线程的框架程序

  1. 工程文件


#-------------------------------------------------
#
# Project created by QtCreator 2023-03-23T21:21:21
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testMultiThread
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        cmyobject.cpp \
        cmyobject2.cpp \
        creadcodesbusiness.cpp \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        cmyobject.h \
        cmyobject2.h \
        creadcodesbusiness.h \
        mainwindow.h

FORMS += \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
  1. 一个组件类


#ifndef CMYOBJECT_H
#define CMYOBJECT_H

#include <QObject>
#include <QTimer>

class CMyObject : public QObject
{
    Q_OBJECT
public:
    explicit CMyObject(QObject *parent = nullptr);
    ~CMyObject();

    QTimer *timer;

signals:
    void signal2ReadCodeBusiness();

public slots:
    void slotStart();

    void slot4ReadCodesBusiness();
};

#endif // CMYOBJECT_H

#pragma execution_character_set("utf-8")

#include "cmyobject.h"
#include <QDebug>
#include <QThread>
#include <QDateTime>

CMyObject::CMyObject(QObject *parent) : QObject(parent)
{

}

CMyObject::~CMyObject()
{
    qDebug()<<"执行了MyObject的析构";
}

void CMyObject::slotStart()
{
    qDebug()<<"子线程 id"<<QThread::currentThreadId()<<QDateTime::currentDateTime();
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        for(int i = 0;i<5;i++)
        {
            QThread::sleep(1);
        }
        //qDebug()<<"计时器执行了"<<QDateTime::currentDateTime();

        emit signal2ReadCodeBusiness();

    });
    timer->start(2000);
}

void CMyObject::slot4ReadCodesBusiness()
{
    qDebug() << "object1 received read code business";
}
  1. 另一个组件类


#ifndef CMYOBJECT2_H
#define CMYOBJECT2_H

#include <QObject>
#include <QTimer>

class CMyObject2 : public QObject
{
    Q_OBJECT
public:
    explicit CMyObject2(QObject *parent = nullptr);
    ~CMyObject2();

    QTimer *timer;

signals:
    void signal2ReadCodeBusiness();

public slots:
    void slotStart2();

    void slot4ReadCodesBusiness();
};
#endif // CMYOBJECT2_H

#pragma execution_character_set("utf-8")

#include "cmyobject2.h"
#include <QDebug>
#include <QThread>
#include <QDateTime>

CMyObject2::CMyObject2(QObject *parent) : QObject(parent)
{

}

CMyObject2::~CMyObject2()
{
    qDebug()<<"执行了MyObject2的析构";
}

void CMyObject2::slotStart2()
{
    qDebug()<<"子线程 id"<<QThread::currentThreadId()<<QDateTime::currentDateTime();
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        for(int i = 0;i<5;i++)
        {
            QThread::sleep(1);
        }
        //qDebug()<<"计时器执行了"<<QDateTime::currentDateTime();

        emit signal2ReadCodeBusiness();
    });
    timer->start(2000);
}

void CMyObject2::slot4ReadCodesBusiness()
{
    qDebug() << "object2 received read code business";
}

  1. 业务调度类


#ifndef CREADCODESBUSINESS_H
#define CREADCODESBUSINESS_H

#include <QObject>
#include <QTimer>

class CReadCodesBusiness : public QObject
{
    Q_OBJECT
public:
    explicit CReadCodesBusiness(QObject *parent = nullptr);
    ~CReadCodesBusiness();

    QTimer *timer;

signals:
    //signal defined for signal from object1 thread
    void signal2Object1();

    //signal defined for signal from object2 thread
    void signal2Object2();

public slots:
    //slot defined for signal from main thread
    void slotStartReadCodes();

    //slot defined for signal from object1 thread
    void slotServeObject1();

    //slot defined for signal from object2 thread
    void slotServeObject2();




};

#endif // CREADCODESBUSINESS_H

#pragma execution_character_set("utf-8")

#include "creadcodesbusiness.h"
#include <QDebug>
#include <QThread>
#include <QDateTime>

CReadCodesBusiness::CReadCodesBusiness(QObject *parent) : QObject(parent)
{

}

CReadCodesBusiness::~CReadCodesBusiness()
{
    qDebug()<<"执行了CReadCodesBusiness的析构";
}

void CReadCodesBusiness::slotStartReadCodes()
{
    qDebug()<<"子线程 id"<<QThread::currentThreadId()<<QDateTime::currentDateTime();
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        for(int i = 0;i<5;i++)
        {
            QThread::sleep(1);
        }
        //qDebug()<<"计时器执行了"<<QDateTime::currentDateTime();

        emit signal2Object1();

        QThread::sleep(2);

        emit signal2Object2();


    });
    timer->start(2000);
}


//slot defined for signal from object1 thread
void CReadCodesBusiness::slotServeObject1()
{
    qDebug() << "Message from Object1 arrived";
}

//slot defined for signal from object2 thread
void CReadCodesBusiness::slotServeObject2()
{
    qDebug() << "Message from Object2 arrived";
}

  1. 主程序


#include "mainwindow.h"
#include <QApplication>

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

    return a.exec();
}

  1. 主界面程序


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "cmyobject.h"
#include "cmyobject2.h"

#include "creadcodesbusiness.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    QThread *thread;
    CMyObject *myobject;

    QThread *thread2;
    CMyObject2 *myobject2;

    QThread *pthreadReadCodesBusiness;
    CReadCodesBusiness *pclassReadCodesBusiness;

signals:
    void signalStart();
    void signalStart2();

    void signalStartReadCodeBusiness();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

#pragma execution_character_set("utf-8")

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QThread>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    thread = new QThread;
    myobject = new CMyObject;
    myobject->moveToThread(thread);
    connect(this,&MainWindow::signalStart,myobject,&CMyObject::slotStart);
    connect(thread,&QThread::finished,myobject,&QObject::deleteLater);

    thread2 = new QThread;
    myobject2 = new CMyObject2;
    myobject2->moveToThread(thread2);
    connect(this,&MainWindow::signalStart2,myobject2,&CMyObject2::slotStart2);
    connect(thread2,&QThread::finished,myobject2,&QObject::deleteLater);

    pthreadReadCodesBusiness = new QThread;
    pclassReadCodesBusiness = new CReadCodesBusiness;
    pclassReadCodesBusiness->moveToThread(pthreadReadCodesBusiness);
    connect(this,&MainWindow::signalStartReadCodeBusiness,pclassReadCodesBusiness,&CReadCodesBusiness::slotStartReadCodes);
    connect(pthreadReadCodesBusiness,&QThread::finished,pclassReadCodesBusiness,&QObject::deleteLater);

    //components --> business
    connect(myobject,&CMyObject::signal2ReadCodeBusiness,pclassReadCodesBusiness,&CReadCodesBusiness::slotServeObject1);
    connect(myobject2,&CMyObject2::signal2ReadCodeBusiness,pclassReadCodesBusiness,&CReadCodesBusiness::slotServeObject2);

    //business --> components
    connect(pclassReadCodesBusiness,&CReadCodesBusiness::signal2Object1,myobject,&CMyObject::slot4ReadCodesBusiness);
    connect(pclassReadCodesBusiness,&CReadCodesBusiness::signal2Object2,myobject2,&CMyObject2::slot4ReadCodesBusiness);

}

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

void MainWindow::on_pushButton_clicked()
{

    qDebug()<<"点击了开始,主线程id"<<QThread::currentThreadId();

    thread->start();

    emit signalStart();
}

void MainWindow::on_pushButton_2_clicked()
{

}

void MainWindow::on_pushButton_3_clicked()
{

    qDebug()<<"点击了开始,主线程id"<<QThread::currentThreadId();

    thread2->start();

    emit signalStart2();
}

void MainWindow::on_pushButton_4_clicked()
{

}

void MainWindow::on_pushButton_5_clicked()
{
    qDebug()<<"点击了开始,主线程id"<<QThread::currentThreadId();


    pthreadReadCodesBusiness->start();

    emit signalStartReadCodeBusiness();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值