Semaforo SCXML (statico, widget)
Un'applicazione basata su widget che utilizza una macchina a stati compilata per implementare un semaforo.

L'esempio "Semaforo" (statico) 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 Widgets.
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.
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 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(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 header e file sorgente per la compilazione.
Istanziazione della macchina a stati
Istanziamo la classe TrafficLightStateMachine generata nel file trafficlight-widgets-static.cpp, come segue:
#include "statemachine.h"
#include "trafficlight.h"
#include <QtWidgets/qapplication.h>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
TrafficLightStateMachine machine;
TrafficLight widget(&machine);
widget.show();
machine.start();
return app.exec();
}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 allo 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:
machine->connectToState(u"red"_s, widget->redLight(), &LightWidget::switchLight);
machine->connectToState(u"redGoingGreen"_s, widget->redLight(), &LightWidget::switchLight);
machine->connectToState(u"yellow"_s, widget->yellowLight(), &LightWidget::switchLight);
machine->connectToState(u"blinking"_s, widget->yellowLight(), &LightWidget::switchLight);
machine->connectToState(u"green"_s, widget->greenLight(), &LightWidget::switchLight);© 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.