SCXML 调用
调用已编译的嵌套状态机。

调用演示如何使用生成的嵌套状态机的 <invoke> 元素,其中 SCXML 文件编译为 C++ 类。该 <invoke> 元素用于创建外部服务的实例。
运行示例
要从 Qt Creator 运行示例,请打开 欢迎 模式并从 示例 中选择示例。更多信息请访问 构建和运行示例。
调用状态机
在 statemachine.scxml 中,我们指定一个名为 DirectionsStateMachine 且类型为 http://www.w3.org/TR/scxml/ 的状态机以调用
<scxml
xmlns="http://www.w3.org/2005/07/scxml"
version="1.0"
name="DirectionsStateMachine"
initial="anyplace"
>
<state id="anyplace">
<transition event="goNowhere" target="nowhere"/>
<transition event="goSomewhere" target="somewhere"/>
<state id="nowhere"/>
<state id="somewhere">
<invoke type="http://www.w3.org/TR/scxml/">
<content>
<scxml name="anywhere" version="1.0">
<state id="here">
<transition event="goThere" target="there"/>
</state>
<state id="there">
<transition event="goHere" target="here"/>
</state>
</scxml>
</content>
</invoke>
</state>
</state>
</scxml>编译状态机
我们通过向示例的构建文件中添加以下行链接到 Qt SCXML 模块。
invoke.pro 当使用 qmake 时:
QT += qml scxml
然后指定要编译的状态机
STATECHARTS = statemachine.scxml
CMakeLists.txt 当使用 cmake 时:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
target_link_libraries(invoke PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Qml
Qt6::Scxml
)然后指定要编译的状态机
qt6_add_statecharts(invoke
statemachine.scxml
)使用 STATECHARTS 或 qt6_add_statecharts 指令调用 Qt SCXML 编译器,即 qscxmlc,它会自动运行以生成 statemachine.h 和 statemachine.cpp,然后被适当地添加为编译的头部和源文件。
将状态机声明为 QML 元素
状态机声明为 QML 元素如下
struct DirectionsStateMachineRegistration { Q_GADGET QML_FOREIGN(DirectionsStateMachine) QML_NAMED_ELEMENT(DirectionsStateMachine) QML_ADDED_IN_VERSION(1, 0) };
实例化状态机
我们在 MainView.qml 文件中实例化生成的 DirectionsStateMachine 元素,如下所示
DirectionsStateMachine { id: stateMachine running: true }
© 2024 Qt 公司有限公司。本文件中包含的文档贡献的版权属于各自的所有者。本文件提供的文档根据免费软件基金会发布的 GNU 自由文档许可 1.3 版 许可。Qt 及相关徽标是芬兰的 Qt 公司及/或其他国家和地区注册的商标。所有其他商标均为其各自所有者的财产。