一、状态机类文章来源地址https://www.toymoban.com/news/detail-536189.html
- 头文件 MyStateMachine.h
#ifndef MYSTATEMACHINE_H
#define MYSTATEMACHINE_H
#include <QStateMachine>
#include "ActionTask.h"
#include <QObject>
#include <QFinalState>
class MyStateMachine:public QObject
{
Q_OBJECT
public:
explicit MyStateMachine(QObject *parent = nullptr);
~MyStateMachine();
signals:
void sigToWorkState();
void sigToIdleState();
public slots:
void onStart();
void onStop();
void onTask();
void onToIdleState();
private:
void initSlot();
private:
QThread* m_thread;
ActionTask* m_worker;
QState* workState;
QState* idleState;
QFinalState *finalState;
QStateMachine *m_achine;
};
#endif // MYSTATEMACHINE_H
- 状态机 cpp
#include "MyStateMachine.h"
#include "qdebug.h"
#include "qeventloop.h"
#include <QEvent>
#include "global.h"
MyStateMachine::MyStateMachine(QObject *parent)
: QObject{
parent}
{
// 创建一个子线程 for state1
m_thread = new QThread();
m_worker = new ActionTask();
m_worker->moveToThread(m_thread);
// 创建状态机
m_achine = new QStateMachine();
workState = new QState();
idleState = new QState();
finalState = new QFinalState();
// 将任务完成状态添加到工作状态中
idleState->addTransition(this, &MyStateMachine::sigToWorkState, workState);
workState->addTransition(this, &MyStateMachine::sigToIdleState, idleState);
workState->addTransition(m_worker, &ActionTask::sigFinished, idleState);
connect(finalState, &QState::exited, this, [this](){
emit gSignal.sigLog
文章来源:https://www.toymoban.com/news/detail-536189.html
到了这里,关于qt 线程状态机实现并发自动任务的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!