简介
使用moveToThread函数的流程如下:
1、创建一个类继承自QObject类或其子类,并在其中定义所要执行的多个任务,执行多个任务就要定义相应的信号。
2、任务通过moveToThread指定所要执行的线程。
3、线程通过start启动
4、通过信号与槽机制触发线程的执行
为什么要用
适用场景:
- 适合单次任务执行,即有点像懒人,触发一下,干一次活
- 适合干完活,需要主动推送一个通知
- 适合用于简化多线程中,对数据安全的保护
它不适用的场景:
- 不适用高频率任务,即跑完一个任务,可能没有时间休息,持续跑
- 执行高频率任务,还是需要使用重写QThread::run()的方式,来实现
示例一个循环读取进度条的模型
1.首先定义我们的worker
头文件
//
// Created by zhe.xiao on 2023/12/05.
//
#pragma once
#include "std_include.h"
#include <QObject>
class OperationWorker : public QObject
{
Q_OBJECT
public:
explicit OperationWorker(QObject* parent = nullptr);
~OperationWorker();
signals:
void SendRecordImportProgress(QString ecgPath);
public slots:
void ProcessRecordImportProgress(QString ecgPath);
};
cpp文件
//
// Created by zhe.xiao on 2023/12/05.
//
#include "operation_worker.h"
#include <QThread>
OperationWorker::OperationWorker(QObject* parent) : QObject(parent)
{
}
OperationWorker::~OperationWorker()
{
}
void OperationWorker::ProcessRecordImportProgress(QString ecgPath)
{
qDebug("===OperationWorker ProcessRecordImportProgress, ecgPath=%s", ecgPath.toStdString().c_str());
int i = 0;
while (i <= 100)
{
std::string percent = std::to_string(i) + "%";
emit SendRecordImportProgress(QString(percent.c_str()));
i += 15;
}
}
2.定义一个控制器,专门来控制worker
头文件
//
// Created by zhe.xiao on 2023/12/05.
//
#pragma once
#include "std_include.h"
#include "operation_worker.h"
#include <QObject>
#include <QThread>
class OperationController : public QObject
{
Q_OBJECT
public:
explicit OperationController(QObject* parent = nullptr);
~OperationController() override;
//触发读取记录仪进度条
void StartRecordImportProgress();
signals:
void TriggerRecordImportProgress(QString ecgPath);
public slots:
void ReceiveRecordImportProgress(QString percent);
private:
QThread m_workThread;
};
cpp文件
//
// Created by zhe.xiao on 2023/12/05.
//
#include "operation_controller.h"
OperationController::OperationController(QObject* parent) : QObject(parent)
{
auto operationWorker = new OperationWorker();
operationWorker->moveToThread(&m_workThread);
//绑定事件
connect(&m_workThread, &QThread::finished, operationWorker, &QObject::deleteLater);
connect(this, &OperationController::TriggerRecordImportProgress, operationWorker, &OperationWorker::ProcessRecordImportProgress);
connect(operationWorker, &OperationWorker::SendRecordImportProgress, this, &OperationController::ReceiveRecordImportProgress);
//线程启动
m_workThread.start();
}
OperationController::~OperationController()
{
m_workThread.quit();
m_workThread.wait();
}
void OperationController::StartRecordImportProgress()
{
emit TriggerRecordImportProgress("xiaozhe");
}
void OperationController::ReceiveRecordImportProgress(QString percent)
{
qDebug("===OperationController ReceiveRecordImportProgress, percent=%s", percent.toStdString().c_str());
}
注:由于Worker是QObject对象,同时通过connect(&thread, &QThread::finished, operationWorker, &QObject::deleteLater)建立信号槽;当线程释放时,会自动删除Worker对象,并不会内存泄漏。
3.外部函数触发执行
int OperationRecorderImport::Test()
{
auto operationController = new OperationController();;
operationController->StartRecordImportProgress();
return OPERATION_SUCCESS;
}
打印如下: