SCXML Invoke
Richiamare una macchina a stati annidata compilata.

Invoke illustra come utilizzare l'elemento ` <invoke> ` con macchine a stati annidate generate, in cui il file SCXML viene compilato in una classe C++. L'elemento ` <invoke> ` viene utilizzato per creare un'istanza di un servizio esterno.
Esecuzione dell'esempio
È possibile eseguire l'esempio da:
- Qt Creator
Aprire la modalità Welcome e selezionare l'esempio da Examples. Per ulteriori informazioni, consultare Qt Creator: Tutorial: Compilazione ed esecuzione.
- Qt Extension for Visual Studio Code
Eseguire il comando " Qt: Open Qt examples " da Command Palette e selezionare l'esempio dall'elenco. Per ulteriori informazioni, consultare Qt Extension for Visual Studio Code: Tutorial: Compilazione ed esecuzione.
Chiamata della macchina a stati
Nel file statemachine.scxml, specifichiamo una macchina a stati denominata DirectionsStateMachine di tipo http://www.w3.org/TR/scxml/ da richiamare:
<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>Compilazione della macchina a stati
Si effettua il collegamento al modulo Qt SCXML aggiungendo le seguenti righe ai file di compilazione dell’esempio.
invoke.pro quando si utilizza qmake:
QT += qml scxmlSi specifica quindi la macchina a stati da compilare:
STATECHARTS = statemachine.scxmlCMakeLists.txt quando si utilizza cmake:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
target_link_libraries(invoke PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Qml
Qt6::Scxml
)Si specifica quindi la macchina a stati da compilare:
qt6_add_statecharts(invoke
statemachine.scxml
)Le direttive STATECHARTS o qt6_add_statecharts richiamano il compilatore Qt SCXML, qscxmlc, che viene eseguito automaticamente per generare statemachine.h e statemachine.cpp, i quali vengono poi aggiunti opportunamente come file di intestazione e sorgenti per la compilazione.
Dichiarazione della macchina a stati come elemento QML
La macchina a stati viene dichiarata come elemento QML come segue:
struct DirectionsStateMachineRegistration
{
Q_GADGET
QML_FOREIGN(DirectionsStateMachine)
QML_NAMED_ELEMENT(DirectionsStateMachine)
QML_ADDED_IN_VERSION(1, 0)
};Istanziamento della macchina a stati
Istanziamo l’elemento DirectionsStateMachine generato nel file MainView.qml, come segue:
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.