SCXML Invoke
Invoca una máquina de estados anidada compilada.

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:
- Qt Creator
Abre el modo « Welcome » y selecciona el ejemplo en « Examples ». Para obtener más información, consulta Qt Creator: Tutorial: Compilar y ejecutar.
- Qt Extension for Visual Studio Code
Ejecute el comando « Qt: Open Qt examples » desde Command Palette y seleccione el ejemplo de la lista. Para obtener más información, consulte Qt Extension for Visual Studio Code: Tutorial: Compilar y ejecutar.
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 scxmlA continuación, especificamos la máquina de estados que se va a compilar:
STATECHARTS = statemachine.scxmlCMakeLists.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
}© 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.