StateMachine QML Type
提供分层有限状态机。更多
Import Statement: | import QtQml.StateMachine 6.9 |
Inherits: |
属性
- errorString : string
- globalRestorePolicy : enumeration
- running : bool
信号
方法
详细说明
StateMachine 基于Statecharts 的概念和符号。StateMachine 是Qt State Machine QML API的一部分。
状态机管理一组状态和这些状态之间的转换;这些状态和转换定义了一个状态图。一旦建立了状态图,状态机就可以执行它。StateMachine 的执行算法基于状态图 XML(SCXML)算法。该框架的概述提供了几种状态图和构建状态图的代码。
在机器启动之前,必须设置initialState 。初始状态是机器启动时进入的状态。然后可以将运行属性设为 true 或将状态机设为start() 。进入初始状态时,会发出 started 信号。
状态机处理事件并进行转换,直到进入顶层最终状态;然后状态机发出 finished() 信号。也可以明确地stop() 状态机(也可以将 running 属性设为 false)。在这种情况下,状态机会发出 stopped 信号。
使用示例
下面的代码段显示了一个状态机,当点击按钮时,状态机将结束运行:
import QtQuick import QtQml.StateMachine as DSM Rectangle { Button { anchors.fill: parent id: button text: "Finish state" DSM.StateMachine { id: stateMachine initialState: state running: true DSM.State { id: state DSM.SignalTransition { targetState: finalState signal: button.clicked } } DSM.FinalState { id: finalState } onFinished: Qt.quit() } } }
如果遇到错误,机器会查找errorState ,如果有,就会进入该状态。进入错误状态后,可以使用 error() 检索错误类型。进入错误状态后,状态图的执行不会停止。如果没有错误状态适用于错误状态,机器将停止执行,并将错误信息打印到控制台。
警告: 将状态机的子模式(childMode)设置为QState::ExclusiveStates 以外的任何值,都会导致状态机无效,并可能导致不正确的行为。
另请参阅 QAbstractState,State,SignalTransition,TimeoutTransition,HistoryState 和Qt State Machine QML 指南。
属性文档
errorString : string |
此状态机的错误字符串。
globalRestorePolicy : enumeration |
信号文档
started() |
当状态机进入初始状态(State::initialState )时,会发出该信号。
注: 相应的处理程序是onStarted
。
另请参见 running,start() 和State::finished 。
stopped() |
方法文档
start() |
启动状态机。机器将重置配置并过渡到初始状态。当进入最终顶层状态(FinalState )时,机器将发出 finished() 信号。
注意: 如果没有正在运行的事件循环,如使用QCoreApplication::exec() 或QApplication::exec() 启动的主应用程序事件循环,状态机将无法运行。
另请参阅 started,State::finished,stop(),State::initialState, 和running 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.