이 페이지에서

SCXML 신호등 (정적, 위젯)

컴파일된 상태 머신을 사용하여 신호등을 구현하는 위젯 기반 애플리케이션입니다.

녹색 불이 켜진 신호등

'신호등 예제(정적) '는 클래스로 컴파일된 상태 머신 내의 상태에 있는 활성 속성에 연결하는 방법을 보여줍니다.

UI는 Qt Widgets 를 사용하여 생성됩니다.

예제 실행

다음 위치에서 예제를 실행할 수 있습니다.

상태 머신 컴파일

예제의 빌드 파일에 다음 줄을 추가하여 Qt SCXML 모듈을 링크합니다.

qmake를 사용할 때.pro 파일:
QT += qml scxml

그런 다음 컴파일할 상태 머신을 지정합니다:

STATECHARTS = statemachine.scxml
cmake를 사용할 때의CMakeLists.txt:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
target_link_libraries(trafficlight-qml-simple PRIVATE
    Qt::Core
    Qt::Gui
    Qt::Qml
    Qt::Scxml
)

그런 다음 컴파일할 상태 머신을 지정합니다:

qt6_add_statecharts(trafficlight-qml-simple
    statemachine.scxml
)

상태 차트 지시어인 STATECHARTS 또는 qt6_add_statecharts는 Qt SCXML 컴파일러( qscxmlc)를 호출하며, 이 컴파일러는 자동으로 실행되어 statemachine.h 및 statemachine.cpp 파일을 생성합니다. 생성된 파일들은 컴파일을 위해 헤더 파일과 소스 파일로 적절히 추가됩니다.

상태 머신 인스턴스화

생성된 TrafficLightStateMachine 클래스는 trafficlight-widgets-static.cpp 파일에서 다음과 같이 인스턴스화합니다:

#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();
}

상태 연결

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);

예제 프로젝트 @ code.qt.io

© 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.