En esta página

SCXML Invoke

Invoca una máquina de estados anidada compilada.

Ventana con cuatro paneles: «No vayas a ningún sitio», «Ve a algún sitio», «Ve allí» y «Ve aquí»

Invoke muestra cómo utilizar el elemento « <invoke> » con máquinas de estados anidadas generadas, en las que el archivo SCXML se compila en una clase de C++. El elemento « <invoke> » se utiliza para crear una instancia de un servicio externo.

Ejecución del ejemplo

Puede ejecutar el ejemplo desde:

Iniciar la máquina de estados

En statemachine.scxml, especificamos una máquina de estados con el nombre DirectionsStateMachine, de tipo http://www.w3.org/TR/scxml/, para invocarla:

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

Compilación de la máquina de estados

Enlazamos con el módulo « Qt SCXML » añadiendo las siguientes líneas a los archivos de compilación del ejemplo.

invoke.pro al utilizar qmake:
QT += qml scxml

A continuación, especificamos la máquina de estados que se va a compilar:

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

target_link_libraries(invoke PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Qml
    Qt6::Scxml
)

A continuación, especificamos la máquina de estados que se va a compilar:

qt6_add_statecharts(invoke
    statemachine.scxml
)

Las directivas de diagramas de estados STATECHARTS o qt6_add_statecharts invocan el compilador Qt SCXML, qscxmlc, que se ejecuta automáticamente para generar statemachine.h y statemachine.cpp, que luego se añaden adecuadamente como encabezados y archivos fuente para la compilación.

Declaración de la máquina de estados como elemento QML

La máquina de estados se declara como un elemento QML de la siguiente manera:

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

Instanciación de la máquina de estados

Instanciamos el elemento ` DirectionsStateMachine ` generado en el archivo ` MainView.qml `, de la siguiente manera:

    DirectionsStateMachine {
        id: stateMachine
        running: true
    }

Proyecto de ejemplo en code.qt.io

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