A QT multi-threaded framework program

  1. Project Files

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

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT + = widgets

TARGET = WeldGuide
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. a component class

#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()<<"Executed the destruction of MyObject";
}

void CMyObject::slotStart()
{
    qDebug()<<"sub-thread 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()<<"The timer has executed"<<QDateTime::currentDateTime();

        emit signal2ReadCodeBusiness();

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

void CMyObject::slot4ReadCodesBusiness()
{
    qDebug() << "object1 received read code business";
}
  1. another component class

#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()<<"Executed the destruction of MyObject2";
}

void CMyObject2::slotStart2()
{
    qDebug()<<"sub-thread 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()<<"The timer has executed"<<QDateTime::currentDateTime();

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

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

#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()<<"Executed the destruction of CReadCodesBusiness";
}

void CReadCodesBusiness::slotStartReadCodes()
{
    qDebug()<<"sub-thread 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()<<"The timer has executed"<<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. main program

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

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

    return a.exec();
}
  1. main interface program

#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, & amp; QThread::finished, myobject, & amp; QObject::deleteLater);

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

    pthreadReadCodesBusiness = new QThread;
    pclassReadCodesBusiness = new CReadCodesBusiness;
    pclassReadCodesBusiness->moveToThread(pthreadReadCodesBusiness);
    connect(this, &MainWindow::signalStartReadCodeBusiness,pclassReadCodesBusiness, &CReadCodesBusiness::slotStartReadCodes);
    connect(pthreadReadCodesBusiness, & amp; QThread::finished, pclassReadCodesBusiness, & amp; 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()<<"Clicked to start, the main thread id"<<QThread::currentThreadId();

    thread->start();

    emit signalStart();
}

void MainWindow::on_pushButton_2_clicked()
{

}

void MainWindow::on_pushButton_3_clicked()
{

    qDebug()<<"Clicked to start, the main thread id"<<QThread::currentThreadId();

    thread2->start();

    emit signalStart2();
}

void MainWindow::on_pushButton_4_clicked()
{

}

void MainWindow::on_pushButton_5_clicked()
{
    qDebug()<<"Clicked to start, the main thread id"<<QThread::currentThreadId();


    pthreadReadCodesBusiness->start();

    emit signalStartReadCodeBusiness();
}