Semáforo SCXML (estático, widgets)
Una aplicación basada en widgets que utiliza una máquina de estados compilada para implementar un semáforo.

El ejemplo «Semáforo» (estático) muestra cómo conectarse a las propiedades activas de un estado en una máquina de estados compilada en una clase.
La interfaz de usuario se crea utilizando Qt Widgets.
Ejecución del ejemplo
Puedes ejecutar el ejemplo desde:
- Qt Creator
Abre el modo « Welcome » y selecciona el ejemplo en « Examples ». Para obtener más información, consulta Qt Creator: Tutorial: Compilar y ejecutar.
- Qt Extension for Visual Studio Code
Ejecute el comando « Qt: Open Qt examples » desde Command Palette y seleccione el ejemplo de la lista. Para obtener más información, consulte Qt Extension for Visual Studio Code: Tutorial: Compilar y ejecutar.
Compilación de la máquina de estados
Enlazamos con el módulo « Qt SCXML » añadiendo las siguientes líneas a los archivos de compilación del ejemplo.
.pro al utilizar qmake:
QT += qml scxmlA continuación, especificamos la máquina de estados que se va a compilar:
STATECHARTS = statemachine.scxmlCMakeLists.txt cuando se utiliza 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
)A continuación, especificamos la máquina de estados que se va a compilar:
qt6_add_statecharts(trafficlight-qml-simple
statemachine.scxml
)Las directivas de diagramas de estados STATECHARTS o qt6_add_statecharts invocan el compilador Qt SCXML, qscxmlc, que se ejecuta automáticamente para generar statemachine.h y statemachine.cpp, que luego se añaden adecuadamente como encabezados y archivos fuente para la compilación.
Instanciación de la máquina de estados
Instanciamos la clase TrafficLightStateMachine generada en el archivo trafficlight-widgets-static.cpp, de la siguiente manera:
#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();
}Conexión a los estados
En el archivo SCXML, especificamos los estados para cada luz: rojo, amarillo y verde. En el elemento ` <onentry> `, especificamos el evento que se enviará al entrar en el estado y el retraso en segundos antes de enviar el evento. En el elemento ` <transition> `, especificamos el evento que desencadena la transición al estado especificado por el 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 los estados de la siguiente manera:
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.