SCXML 신호등 (동적, 위젯)
동적으로 로드되는 상태 머신을 사용하여 신호등을 구현하는 위젯 기반 애플리케이션입니다.

'신호등'은 동적으로 로드되는 상태 머신에서 상태의 활성 속성에 연결하는 방법을 보여줍니다.
UI는 Qt Widgets 를 사용하여 생성됩니다.
예제 실행
다음 위치에서 예제를 실행할 수 있습니다.
- Qt Creator
Welcome 모드를 열고 Examples 에서 예제를 선택하십시오. 자세한 내용은 Qt Creator: 튜토리얼: 빌드 및 실행을 참조하십시오.
- Qt Extension for Visual Studio Code
Command Palette 에서 Qt: Open Qt examples 명령을 실행하고 목록에서 예제를 선택합니다. 자세한 내용은 Qt Extension for Visual Studio Code: 튜토리얼: 빌드 및 실행을 참조하십시오.
상태 머신 동적 로딩
예제의 빌드 파일에 다음 줄을 추가하여 Qt SCXML 모듈을 링크합니다.
qmake를 사용할 경우 .pro 파일에:
QT += widgets scxmlcmake를 사용할 경우 CMakeLists.txt 에 추가:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Scxml Widgets)
target_link_libraries(trafficlight-widgets-dynamic PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Scxml
Qt6::Widgets
)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;
}그런 다음 인스턴스를 생성합니다:
TrafficLight widget(machine);
widget.show();
machine->setParent(&widget);
machine->start();
return app.exec();
}상태 연결
SCXML 파일에서 각 신호등에 대한 상태(빨간색, 노란색, 녹색)를 지정합니다. ` <onentry> ` 요소에서는 해당 상태로 진입할 때 전송할 이벤트와 이벤트 전송 전의 지연 시간(초 단위)을 지정합니다. ` <transition> ` 요소에서는 ` 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>상태 간 연결은 다음과 같이 수행합니다:
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.