当前位置: 首页 > ops >正文

Qt图形视图框架5-状态机框架

1. Qt状态机框架:原理、实现与应用

  • 1. Qt状态机框架:原理、实现与应用
    • 1.1. 状态机框架概述
    • 1.2. 状态机基本概念
    • 1.3. 状态机实现示例
      • 1.3.1. 状态机代码实现
      • 1.3.2. 状态图可视化
    • 1.4. 状态机高级特性
    • 1.5. 状态机框架应用场景
    • 1.6. 总结

状态机是现代软件开发中处理复杂交互逻辑的强大工具,尤其适用于事件驱动的系统。Qt框架提供了完善的状态机API,使开发者能够以图形化模型方式定义和管理应用程序状态,极大简化了复杂逻辑的实现。

1.1. 状态机框架概述

状态机框架为系统如何响应外部激励提供了图形化模型,通过定义系统可能进入的状态以及状态间的转换规则,直观表达系统行为。这种模型的核心优势在于:

  • 事件驱动响应:系统行为不仅依赖当前事件,还与历史状态相关
  • 分层结构设计:状态可以嵌套,形成层次化状态图
  • 信号驱动转换:与Qt元对象系统深度集成,状态转换可由信号触发
  • 异步执行机制:作为应用程序事件循环的一部分运行

1.2. 状态机基本概念

  1. 状态(State):系统在某一时刻的特定条件或模式
  2. 转换(Transition):状态之间的切换规则
  3. 事件(Event):触发状态转换的外部激励
  4. 初始状态:状态机启动后自动进入的状态
  5. 终止状态:导致状态机停止运行的特殊状态

1.3. 状态机实现示例

下面通过一个简单示例演示如何使用Qt状态机框架实现一个由按钮控制的三状态循环系统。

1.3.1. 状态机代码实现

#include <QApplication>
#include <QPushButton>
#include <QStateMachine>
#include <QState>
#include <QFinalState>int main(int argc, char *argv[])
{QApplication a(argc, argv);// 创建按钮和状态机QPushButton button("Click me");button.show();QStateMachine machine;// 创建三个状态QState *s1 = new QState(&machine);QState *s2 = new QState(&machine);QState *s3 = new QState(&machine);// 为每个状态设置按钮文本属性s1->assignProperty(&button, "text", "State 1");s2->assignProperty(&button, "text", "State 2");s3->assignProperty(&button, "text", "State 3");// 定义状态转换(循环切换)s1->addTransition(&button, &QPushButton::clicked, s2);s2->addTransition(&button, &QPushButton::clicked, s3);s3->addTransition(&button, &QPushButton::clicked, s1);// 设置初始状态machine.setInitialState(s1);// 启动状态机machine.start();return a.exec();
}

1.3.2. 状态图可视化

    s1 ────(button.clicked)────► s2▲                          ││                          │└────(button.clicked)─────┘││└───(button.clicked)────► s3│└───────────────────┘

1.4. 状态机高级特性

  1. 状态进入/退出事件

状态机提供了entered()exited()信号,可用于在状态变化时执行特定操作:

// 在进入s3状态时最小化按钮
QObject::connect(s3, &QState::entered, &button, &QPushButton::showMinimized);// 在退出s2状态时打印日志
QObject::connect(s2, &QState::exited, [](){qDebug() << "Exiting state s2";
});
  1. 终止状态设置

若需状态机在完成特定任务后自动停止,可设置终止状态:

QFinalState *finalState = new QFinalState(&machine);
s3->addTransition(&button, &QPushButton::clicked, finalState);// 连接finished信号处理状态机停止事件
QObject::connect(&machine, &QStateMachine::finished, &a, &QApplication::quit);
  1. 分层状态设计

状态可以嵌套形成层次结构,子状态共享父状态的属性:

QState *parentState = new QState(&machine);
QState *childState1 = new QState(parentState);
QState *childState2 = new QState(parentState);parentState->setInitialState(childState1);

下面看一个示例,基于Qt状态机框架实现的打印机状态管理系统示例。这个示例展示了如何使用状态嵌套来管理打印机的各种操作模式,包括待机、打印和维护状态,以及打印过程中的子状态。

  1. 状态层次结构

    • 父状态:PrinterOperation
      • 子状态:IdlePrintingMaintenance
      • 子子状态:HeatingBedExtruding(属于Printing状态)
  2. 属性共享

    • 所有子状态自动继承父状态的属性(如温度设置、打印精度)
    • 特定状态可覆盖父状态的属性值(如加热和挤出状态的温度不同)
  3. 状态转换逻辑

    • 通过信号和槽机制触发状态转换
    • 打印过程自动从加热床状态过渡到挤出材料状态,最后回到待机状态
  4. 状态进入/退出事件

    • 在状态转换时执行相应的打印机操作(如开始/停止打印、加热床等)
  5. 可视化界面

    • 显示当前打印机状态
    • 通过按钮控制打印机操作

这个示例展示了如何使用Qt状态机框架的嵌套状态功能来管理复杂的设备操作流程,同时保持代码的模块化和可维护性。

#include <QApplication>
#include <QStateMachine>
#include <QState>
#include <QFinalState>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QWidget>
#include <QDebug>
#include <QObject>
#include <QTimer>// 打印机设备模拟类class Printer : public QObject {Q_OBJECTQ_PROPERTY(int temperature READ temperature WRITE setTemperature NOTIFY temperatureChanged)Q_PROPERTY(double printQuality READ printQuality WRITE setPrintQuality NOTIFY printQualityChanged)Q_PROPERTY(bool heating READ isHeating WRITE setHeating NOTIFY heatingChanged)Q_PROPERTY(bool extruding READ isExtruding WRITE setExtruding NOTIFY extrudingChanged)public:explicit Printer(QObject* parent = nullptr) : QObject(parent),m_temperature(25), m_printQuality(100), m_heating(false), m_extruding(false) {}int temperature() const { return m_temperature; }double printQuality() const { return m_printQuality; }bool isHeating() const { return m_heating; }bool isExtruding() const { return m_extruding; }public slots:void setTemperature(int temp) {if (m_temperature != temp) {m_temperature = temp;emit temperatureChanged(temp);}}void setPrintQuality(double quality) {if (m_printQuality != quality) {m_printQuality = quality;emit printQualityChanged(quality);}}void setHeating(bool heating) {if (m_heating != heating) {m_heating = heating;emit heatingChanged(heating);}}void setExtruding(bool extruding) {if (m_extruding != extruding) {m_extruding = extruding;emit extrudingChanged(extruding);}}// 模拟打印机操作void startPrinting() { qDebug() << "Starting printing process..."; }void stopPrinting() { qDebug() << "Printing stopped."; }void startMaintenance() { qDebug() << "Starting maintenance..."; }void finishMaintenance() { qDebug() << "Maintenance completed."; }void heatBed() { qDebug() << "Heating bed to printing temperature..."; }void extrudeMaterial() { qDebug() << "Extruding material..."; }signals:void temperatureChanged(int temp);void printQualityChanged(double quality);void heatingChanged(bool heating);void extrudingChanged(bool extruding);void printRequested();void maintenanceRequested();void cancelRequested();void bedHeated();void materialExtruded();private:int m_temperature;         // 温度double m_printQuality;     // 打印质量bool m_heating;            // 是否正在加热bool m_extruding;          // 是否正在挤出材料};int main(int argc, char* argv[]) {QApplication a(argc, argv);// 创建主窗口和布局QWidget window;window.setWindowTitle("3D Printer State Machine");QVBoxLayout* layout = new QVBoxLayout(&window);// 创建打印机对象Printer printer;// 创建状态机QStateMachine machine;// 创建父状态: 打印机操作QState* printerOperation = new QState(&machine);// 创建子状态QState* idleState = new QState(printerOperation);QState* printingState = new QState(printerOperation);QState* maintenanceState = new QState(printerOperation);// 在打印状态下创建子子状态QState* heatingBedState = new QState(printingState);QState* extrudingState = new QState(printingState);// 设置初始子状态printerOperation->setInitialState(idleState);printingState->setInitialState(heatingBedState);// 定义状态属性idleState->assignProperty(&printer, "temperature", 25);idleState->assignProperty(&printer, "heating", false);idleState->assignProperty(&printer, "extruding", false);heatingBedState->assignProperty(&printer, "temperature", 60);heatingBedState->assignProperty(&printer, "heating", true);extrudingState->assignProperty(&printer, "temperature", 200);extrudingState->assignProperty(&printer, "heating", true);extrudingState->assignProperty(&printer, "extruding", true);// 状态转换// 待机 -> 打印idleState->addTransition(&printer, &Printer::printRequested, printingState);// 待机 -> 维护idleState->addTransition(&printer, &Printer::maintenanceRequested, maintenanceState);// 打印 -> 待机printingState->addTransition(&printer, &Printer::cancelRequested, idleState);// 维护 -> 待机maintenanceState->addTransition(&printer, &Printer::cancelRequested, idleState);// 打印状态内部转换heatingBedState->addTransition(&printer, &Printer::bedHeated, extrudingState);extrudingState->addTransition(&printer, &Printer::materialExtruded, idleState);// 状态进入事件处理QObject::connect(idleState, &QState::entered, &printer, &Printer::stopPrinting);QObject::connect(printingState, &QState::entered, &printer, &Printer::startPrinting);QObject::connect(maintenanceState, &QState::entered, &printer, &Printer::startMaintenance);QObject::connect(maintenanceState, &QState::exited, &printer, &Printer::finishMaintenance);QObject::connect(heatingBedState, &QState::entered, &printer, &Printer::heatBed);QObject::connect(extrudingState, &QState::entered, &printer, &Printer::extrudeMaterial);// 模拟打印完成QTimer::singleShot(5000, [&printer]() {qDebug() << "Bed heated to target temperature";printer.setHeating(false);emit printer.bedHeated();});QTimer::singleShot(8000, [&printer]() {qDebug() << "Printing completed";printer.setExtruding(false);emit printer.materialExtruded();});// 设置主状态机的初始状态machine.setInitialState(printerOperation);// 启动状态机machine.start();// 创建UI控件QLabel* statusLabel = new QLabel("Printer Status: Idle");layout->addWidget(statusLabel);QPushButton* printButton = new QPushButton("Start Printing");QPushButton* maintenanceButton = new QPushButton("Start Maintenance");QPushButton* cancelButton = new QPushButton("Cancel");layout->addWidget(printButton);layout->addWidget(maintenanceButton);layout->addWidget(cancelButton);// 连接按钮到打印机信号QObject::connect(printButton, &QPushButton::clicked, &printer, &Printer::printRequested);QObject::connect(maintenanceButton, &QPushButton::clicked, &printer, &Printer::maintenanceRequested);QObject::connect(cancelButton, &QPushButton::clicked, &printer, &Printer::cancelRequested);// 显示状态变化QObject::connect(idleState, &QState::entered, [statusLabel]() {statusLabel->setText("Printer Status: Idle");});QObject::connect(printingState, &QState::entered, [statusLabel]() {statusLabel->setText("Printer Status: Printing");});QObject::connect(maintenanceState, &QState::entered, [statusLabel]() {statusLabel->setText("Printer Status: Maintenance");});QObject::connect(heatingBedState, &QState::entered, [statusLabel]() {statusLabel->setText("Printer Status: Heating Bed");});QObject::connect(extrudingState, &QState::entered, [statusLabel]() {statusLabel->setText("Printer Status: Extruding Material");});// 显示窗口window.show();return a.exec();
}#include "main.moc"

1.5. 状态机框架应用场景

  1. 用户界面状态管理:管理复杂界面的不同显示模式
  2. 游戏逻辑控制:实现游戏角色状态、关卡转换等
  3. 通信协议实现:处理网络协议中的各种状态转换
  4. 工作流引擎:定义和执行复杂业务流程

1.6. 总结

Qt状态机框架提供了一种直观且高效的方式来管理事件驱动系统中的复杂状态逻辑。通过图形化模型表达系统行为,开发者可以更清晰地设计和实现复杂交互逻辑,同时提高代码的可维护性和可测试性。

状态机框架的核心优势在于其与Qt元对象系统的深度集成,允许状态转换直接由信号触发,无缝融入Qt应用程序的事件循环体系。通过合理运用分层状态、信号连接和属性分配等机制,可以构建出既强大又灵活的应用程序状态管理系统。

http://www.xdnf.cn/news/15405.html

相关文章:

  • 【Python进阶】深度复制——deepcopy
  • 【人工智能】通过 Dify 构建智能助手
  • JavaScript书写基础和基本数据类型
  • 8:从USB摄像头把声音拿出来--ALSA大佬登场!
  • 算法训练营day18 530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先
  • 迁移学习:知识复用的智能迁移引擎 | 从理论到实践的跨域赋能范式
  • 【前端:Typst】--let关键字的用法
  • 排序树与无序树:数据结构中的有序性探秘
  • 自定义类型 - 联合体与枚举(百度笔试题算法优化)
  • 理解Linux文件系统:从物理存储到统一接口
  • vue3 JavaScript 数据累加 reduce
  • 七、深度学习——RNN
  • 编程语言设计目的与侧重点全解析(主流语言深度总结)
  • 游戏框架笔记
  • 【小白量化智能体】应用5:编写通达信股票交易指标及生成QMT自动交易Python策略程序
  • 控制台打开mysql服务报错解决办法
  • 【STM32】什么在使能寄存器或外设之前必须先打开时钟?
  • 2025js——面试题(8)-http
  • YOLOv11开发流程
  • 为什么资深C++开发者大部分选vector?揭秘背后的硬核性能真相!
  • 【第一章编辑器开发基础第二节编辑器布局_3GUI元素和布局大小(3/4)】
  • SpringMVC3
  • JavaScript进阶篇——第二章 高级特性核心
  • 【笔记】chrome 无法打开特定协议或访问特定协议时卡死
  • Flink窗口处理函数
  • 0-1搭建springboot+vue的教务管理系统(核心源码)
  • Spring Boot 自带的 JavaMail 集成
  • Python在量化投资中的应用
  • 庸才的自我唤醒
  • Rust语言实战:LeetCode算法精解