SCXML Invoke

Invokes a compiled nested state machine.

Invoke demonstrates how to use the <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.

Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Invoking the State Machine

In statemachine.scxml, we specify a state machine with the name DirectionsStateMachine of type to invoke:

    <state id="anyplace">
        <transition event="goNowhere" target="nowhere"/>
        <transition event="goSomewhere" target="somewhere"/>

        <state id="nowhere"/>
        <state id="somewhere">
            <invoke type="">
                    <scxml name="anywhere" version="1.0">
                        <state id="here">
                            <transition event="goThere" target="there"/>
                        <state id="there">
                            <transition event="goHere" target="here"/>

Compiling the State Machine

We link against the Qt SCXML module by adding the following lines to the example's build files. when using qmake:
QT += qml scxml

We then specify the state machine to compile:

STATECHARTS = statemachine.scxml
CMakeLists.txt when using cmake:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)

target_link_libraries(invoke PRIVATE

We then specify the state machine to compile:


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

Instantiating the State Machine

We instantiate the generated DirectionsStateMachine element in the MainView.qml file, as follows:

    DirectionsStateMachine {
        id: stateMachine
        running: true

Example project @

© 2024 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.