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

Traffic Light Example (Static ) demuestra cómo conectarse a las propiedades activas de un estado en una máquina de estados compilada a una clase.
La interfaz de usuario se crea utilizando Qt Widgets.
Ejecutar el ejemplo
Para ejecutar el ejemplo desde Qt Creatorabra el modo Welcome y seleccione el ejemplo de Examples. Para más información, ver Qt Creator: Tutorial: Construir 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 cuando se utiliza qmake:
QT += qml scxml
Especificamos la máquina de estados a compilar:
STATECHARTS = ../trafficlight-common/statemachine.scxml
CMakeLists.txt si se utiliza cmake:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
target_link_libraries(trafficlight-qml-static PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Qml
Qt6::Scxml
)Especificamos la máquina de estados a compilar:
qt6_add_statecharts(trafficlight-qml-static
../trafficlight-common/statemachine.scxml
)Las directivas statechart STATECHARTS o qt6_add_statecharts invocan al compilador Qt SCXML, qscxmlc, que se ejecuta automáticamente para generar statemachine.h y statemachine.cpp, que luego se añaden adecuadamente como cabeceras y fuentes para la compilación.
Instanciación de la máquina de estados
Instanciamos la clase TrafficLightStateMachine generada en el archivo trafficlight-widgets-static.cpp, como sigue:
#include "statemachine.h" #include "../trafficlight-common/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 Estados
En el fichero SCXML, especificamos los estados para cada semáforo: rojo, amarillo y verde. En el elemento <onentry>, especificamos el evento a enviar al entrar en el estado y el retardo 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>Nos conectamos a 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.