このページでは

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.