En esta página

Invocar SCXML

Invoca una máquina de estados anidada compilada.

Ventana con cuatro paneles: Ir a ninguna parte, Ir a algún sitio, Ir allí y Ir aquí

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

Ejecución del ejemplo

Para ejecutar el ejemplo desde Qt Creatorabra el modo Welcome y seleccione el ejemplo de Examples. Para más información, consulte Qt Creator: Tutorial: Construir y ejecutar.

Invocación de la máquina de estados

En statemachine.scxml, especificamos una máquina de estado con el nombre DirectionsStateMachine de tipo http://www.w3.org/TR/scxml/ a invocar:

<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 cuando se utiliza qmake:
QT += qml scxml

Especificamos la máquina de estados a compilar:

STATECHARTS = statemachine.scxml
CMakeLists.txt cuando se utiliza cmake:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)

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

Especificamos la máquina de estados a compilar:

qt6_add_statecharts(invoke
    statemachine.scxml
)

Las directivas statechart STATECHARTS o qt6_add_statecharts invocan al compilador Qt SCXML, qscxmlc, que se ejecuta automáticamente para generar statemachine.h y statemachine.cpp, que luego se añaden adecuadamente como cabeceras y fuentes 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, como sigue:

    DirectionsStateMachine {
        id: stateMachine
        running: true
    }

Proyecto de ejemplo @ 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.