서론
MainWindow에서 2개의 객체를 동시에 실행시켜보자.
흐름도
1. 클래스간의 헤더 파일에서 slots 함수와 signal을 선언 및 cpp파일에서 slots 함수 구현을 해준다.
- 슬롯 함수
void Custom::doWork()
{
for(int i=0; i<5; i++){
QThread::sleep(1);
qDebug() << "[Custom] in thread : " << QThread::currentThread();
}
emit workFinished();
}
2. mainwindow.cpp 파일에서 signal과 slots 함수를 연결해준다.
worker->moveToThread(workerThread);
custom->moveToThread(customThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(customThread, SIGNAL(started()), custom, SLOT(doWork()));
connect(worker, SIGNAL(workFinished()), this, SLOT(on_workFinished()));
connect(custom, SIGNAL(workFinished()), this, SLOT(on_workFinished()));
connect(worker, SIGNAL(workFinished()), workerThread, SLOT(quit()));
connect(custom, SIGNAL(workFinished()), customThread, SLOT(quit()));
connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(customThread, SIGNAL(finished()), custom, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
connect(customThread, SIGNAL(finished()), customThread, SLOT(deleteLater()));
그러면 그림과 같이 실행 순서를 알 수 있다.
전체 코드
Mainwindow
더보기
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "worker.h"
#include "custom.h"
#include <QMainWindow>
#include <QThread>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QThread *workerThread;
QThread *customThread;
Worker *worker;
Custom *custom;
private slots:
void on_startButton_clicked();
void on_workFinished();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, workerThread(new QThread(this))
, customThread(new QThread(this))
, worker(new Worker)
, custom(new Custom)
{
ui->setupUi(this);
worker->moveToThread(workerThread);
custom->moveToThread(customThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(customThread, SIGNAL(started()), custom, SLOT(doWork()));
connect(worker, SIGNAL(workFinished()), this, SLOT(on_workFinished()));
connect(custom, SIGNAL(workFinished()), this, SLOT(on_workFinished()));
connect(worker, SIGNAL(workFinished()), workerThread, SLOT(quit()));
connect(custom, SIGNAL(workFinished()), customThread, SLOT(quit()));
connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(customThread, SIGNAL(finished()), custom, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
connect(customThread, SIGNAL(finished()), customThread, SLOT(deleteLater()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startButton_clicked()
{
workerThread->start();
customThread->start();
}
void MainWindow::on_workFinished()
{
qDebug() << "Work finished in thread : " << QThread::currentThread();
}
worker
더보기
worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
public slots:
void doWork();
signals:
void workFinished();
};
#endif // WORKER_H
worker.cpp
#include "worker.h"
#include <QThread>
#include <QDebug>
Worker::Worker(QObject *parent)
: QObject{parent}
{
}
void Worker::doWork()
{
for(int i=0; i<5; i++){
QThread::sleep(1);
qDebug() << "[Worker] in thread : " << QThread::currentThread();
}
emit workFinished();
}
Custom
더보기
custom.h
#ifndef CUSTOM_H
#define CUSTOM_H
#include <QObject>
class Custom : public QObject
{
Q_OBJECT
public:
explicit Custom(QObject *parent = nullptr);
public slots:
void doWork();
signals:
void workFinished();
};
#endif // CUSTOM_H
custom.cpp
#include "custom.h"
#include <QThread>
#include <QDebug>
Custom::Custom(QObject *parent)
: QObject{parent}
{
}
void Custom::doWork()
{
for(int i=0; i<5; i++){
QThread::sleep(1);
qDebug() << "[Custom] in thread : " << QThread::currentThread();
}
emit workFinished();
}
결과
'🌠Development > QT' 카테고리의 다른 글
QT - 스레드 뮤텍스 (0) | 2024.07.25 |
---|---|
QT - 스레드 동기화(Thread Synchronization) (2) | 2024.07.24 |
QT - Thread (2) [공식문서를 살펴보자] (2) | 2024.07.22 |
Qt - Thread(1) (1) | 2024.07.22 |
QT project - 메모장 구현하기 (0) | 2024.07.19 |