Nesta página

Semáforo SCXML (estático, QML)

Um aplicativo do Qt Quick que utiliza uma máquina de estados compilada para implementar um semáforo.

Semáforo com a luz verde acesa

O Semáforo demonstra como se conectar às propriedades ativas de um estado em uma máquina de estados compilada em uma classe.

A interface do usuário é criada usando o Qt Quick.

Executando o exemplo

Você pode executar o exemplo da seguinte maneira:

Compilando a máquina de estados

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

.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(trafficlight-qml-simple PRIVATE
    Qt::Core
    Qt::Gui
    Qt::Qml
    Qt::Scxml
)

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

qt6_add_statecharts(trafficlight-qml-simple
    statemachine.scxml
)

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

Instanciando a máquina de estados

Instanciamos a máquina de estados da seguinte maneira:

import TrafficLightApplication

Window {
    width: lights.width
    height: lights.height
    visible: true

    Lights {
        id: lights
        stateMachine: TrafficLightStateMachine {
            running: true
        }

Conectando-se aos estados

No arquivo SCXML, especificamos os estados para cada luz: vermelho, amarelo e verde. No elemento ` <onentry> `, especificamos o evento a ser enviado ao entrar no estado e o atraso, em segundos, antes do envio do evento. No elemento ` <transition> `, especificamos o evento que aciona a transição para o estado especificado pelo atributo ` target `:

        <state id="red">
            <onentry>
                <send event="startGoingGreen" delay="3s"/>
            </onentry>
            <transition event="startGoingGreen" target="redGoingGreen"/>
        </state>

        <state id="yellow" initial="greenGoingRed">
            <state id="redGoingGreen">
                <onentry>
                    <send event="goGreen" delay="1s"/>
                </onentry>
                <transition event="goGreen" target="green"/>
            </state>

            <state id="greenGoingRed">
                <onentry>
                    <send event="goRed" delay="1s"/>
                </onentry>
                <transition event="goRed" target="red"/>
            </state>
        </state>

        <state id="green">
            <onentry>
                <send event="startGoingRed" delay="3s"/>
            </onentry>
            <transition event="startGoingRed" target="greenGoingRed"/>
        </state>

Conectamos os estados da seguinte maneira:

    states: [
        State {
            name: "Red"
            when: lights.stateMachine.red

            PropertyChanges { redLight.opacity: 1 }
        },
        State {
            name: "RedGoingGreen"
            when: lights.stateMachine.redGoingGreen

            PropertyChanges { redLight.opacity: 1 }
            PropertyChanges { yellowLight.opacity: 1 }
        },
        State {
            name: "Yellow"
            when: lights.stateMachine.yellow || lights.stateMachine.blinking

            PropertyChanges { yellowLight.opacity: 1 }
        },
        State {
            name: "Green"
            when: lights.stateMachine.green

            PropertyChanges { greenLight.opacity: 1 }
        }
    ]

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.