SCXML Invoke

援引编译嵌套状态机。

援引 演示如何使用 <invoke> element with generated nested state-machines, where the SCXML file is compiled to a C++ class. The <invoke> element is used to create an instance of an external service.

运行范例

要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,拜访 构建和运行范例 .

援引状态机

statemachine.scxml , we specify a state machine with the name DirectionsStateMachine 类型 http://www.w3.org/TR/scxml/ to invoke:

<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>
					

编译状态机

We link against the Qt SCXML module by adding the following lines to the example's build files.

invoke.pro when using qmake:
QT += qml scxml
					

接着,指定要编译的状态机:

STATECHARTS = statemachine.scxml
					
CMakeLists.txt when using 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
)
					

The statechart directives STATECHARTS or qt6_add_statecharts invoke the Qt SCXML Compiler, qscxmlc , which is run automatically to generate statemachine.h and statemachine.cpp , which are then added appropriately as headers and sources for compilation.

Declaring the state machine as QML element

The state machine is declared as a QML element as follows:

struct DirectionsStateMachineRegistration
{
    Q_GADGET
    QML_FOREIGN(DirectionsStateMachine)
    QML_NAMED_ELEMENT(DirectionsStateMachine)
    QML_ADDED_IN_VERSION(1, 0)
};
					

实例化状态机

We instantiate the generated DirectionsStateMachine element in the MainView.qml 文件,如下:

    DirectionsStateMachine {
        id: stateMachine
        running: true
    }
					

范例工程 @ code.qt.io