In questa pagina

Semaforo SCXML (Semplice, QML)

Un'applicazione Qt Quick che utilizza una macchina a stati compilata per implementare un semaforo semplificato.

Semaforo con la luce verde accesa

Traffic Light illustra come collegarsi alle proprietà attive di uno stato in una macchina a stati compilata in una classe.

L'interfaccia utente è stata creata utilizzando Qt Quick.

Esecuzione dell'esempio

È possibile eseguire l'esempio da:

Compilazione della macchina a stati

Effettuiamo il collegamento con il modulo Qt SCXML aggiungendo le seguenti righe ai file di compilazione dell’esempio.

.pro quando si utilizza qmake:
QT += qml scxml

Si specifica quindi la macchina a stati da compilare:

STATECHARTS = statemachine.scxml
CMakeLists.txt quando si utilizza 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
)

Successivamente si specifica la macchina a stati da compilare:

qt6_add_statecharts(trafficlight-qml-simple
    statemachine.scxml
)

Le direttive STATECHARTS o qt6_add_statecharts di Statechart 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.

Istanziamento della macchina a stati

Istanziamo la macchina a stati come segue:

    TrafficLightStateMachine {
        id: stateMachine
        running: true
    }

Collegamento agli stati

Nel file SCXML, specifichiamo gli stati per ciascuna luce: rosso, giallo e verde. Nell’elemento ` <onentry> `, specifichiamo l’evento da inviare all’entrata nello stato e il ritardo in secondi prima dell’invio dell’evento. Nell’elemento ` <transition> `, specifichiamo l’evento che innesca la transizione verso lo stato specificato dall’attributo ` 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>

Colleghiamo gli stati come segue:

        Light {
            anchors.top: parent.top
            anchors.horizontalCenter: parent.horizontalCenter
            color: "red"
            visible: stateMachine.red || stateMachine.redGoingGreen
        }

        Light {
            anchors.centerIn: parent
            color: "yellow"
            visible: stateMachine.yellow || stateMachine.blinking
        }

        Light {
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            color: "green"
            visible: stateMachine.green
        }
    }

Progetto di esempio su @ 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.