Semáforo em SCXML (Dinâmico, Widgets)
Um aplicativo baseado em widgets que utiliza uma máquina de estados carregada dinamicamente para implementar um semáforo.

O Semáforo demonstra como se conectar às propriedades ativas de um estado em uma máquina de estados carregada dinamicamente.
A interface do usuário é criada usando Qt Widgets.
Executando o exemplo
Você pode executar o exemplo da seguinte forma:
- Qt Creator
Abra o modo “ Welcome ” e selecione o exemplo em “ Examples ”. Para mais informações, consulte “Qt Creator ”: Tutorial: Compilar e executar.
- Qt Extension for Visual Studio Code
Execute o comando ` Qt: Open Qt examples ` a partir de Command Palette e selecione o exemplo na lista. Para obter mais informações, consulte Qt Extension for Visual Studio Code: Tutorial: Compilar e executar.
Carregamento dinâmico da máquina de estados
Fazemos a ligação ao módulo Qt SCXML adicionando as seguintes linhas aos arquivos de compilação do exemplo.
No arquivo .pro ao usar o qmake:
QT += widgets scxmlAo CMakeLists.txt ao usar o cmake:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Scxml Widgets)
target_link_libraries(trafficlight-widgets-dynamic PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Scxml
Qt6::Widgets
)Criamos dinamicamente a máquina de estados no arquivo trafficlight-widgets-dynamic.cpp:
#include "trafficlight.h"
#include <QtWidgets/qapplication.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qtextstream.h>
using namespace Qt::Literals::StringLiterals;
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QScxmlStateMachine *machine = QScxmlStateMachine::fromFile(u":statemachine.scxml"_s);
if (!machine->parseErrors().isEmpty()) {
QTextStream errs(stderr, QIODevice::WriteOnly);
const auto errors = machine->parseErrors();
for (const QScxmlError &error : errors) {
errs << error.toString();
}
return -1;
}E, em seguida, a instanciamos:
TrafficLight widget(machine);
widget.show();
machine->setParent(&widget);
machine->start();
return app.exec();
}Conectando-se aos estados
No arquivo SCXML, especificamos os estados para cada semáforo: 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:
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.