Nesta página

SCXML Invoke

Invoca uma máquina de estados aninhada compilada.

Janela com quatro painéis: Não vá a lugar nenhum, Vá a algum lugar, Vá até lá e Vá até aqui

O Invoke demonstra como usar o elemento ` <invoke> ` com máquinas de estado aninhadas geradas, nas quais o arquivo SCXML é compilado em uma classe C++. O elemento ` <invoke> ` é usado para criar uma instância de um serviço externo.

Executando o exemplo

Você pode executar o exemplo a partir de:

Chamando a máquina de estados

No arquivo statemachine.scxml, especificamos uma máquina de estados com o nome DirectionsStateMachine, do tipo http://www.w3.org/TR/scxml/, para ser invocada:

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

Compilando a máquina de estados

Fazemos a ligação com o módulo ` Qt SCXML ` adicionando as seguintes linhas aos arquivos de compilação do exemplo.

invoke.pro ao usar o qmake:
QT += qml scxml

Em seguida, especificamos a máquina de estados a ser compilada:

STATECHARTS = statemachine.scxml
CMakeLists.txt ao usar o cmake:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)

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

Em seguida, especificamos a máquina de estados a ser compilada:

qt6_add_statecharts(invoke
    statemachine.scxml
)

As diretivas de diagramas de estado STATECHARTS ou qt6_add_statecharts invocam o Compilador Qt SCXML, qscxmlc, que é executado automaticamente para gerar os arquivos statemachine.h e statemachine.cpp, os quais são então adicionados adequadamente como cabeçalhos e arquivos-fonte para compilação.

Declarando a máquina de estados como elemento QML

A máquina de estados é declarada como um elemento QML da seguinte forma:

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

Instanciando a máquina de estados

Instanciamos o elemento ` DirectionsStateMachine ` gerado no arquivo ` MainView.qml `, da seguinte forma:

    DirectionsStateMachine {
        id: stateMachine
        running: true
    }

Projeto de exemplo @ 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.