SCXML 信号機 (スタティック、ウィジェット)

コンパイルされたステートマシンを使用して信号機を実装する、ウィジェットベースのアプリケーションです。

Traffic Light Example (Static)は、クラスにコンパイルされたステートマシンの状態のアクティブプロパティに接続する方法を示します。

UI は、Qt Widgets を使用して作成します。

例の実行

から例を実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳細については、Building and Running an Exampleを参照してください。

ステートマシンのコンパイル

サンプルのビルドファイルに以下の行を追加することで、Qt SCXML モジュールにリンクします。

qmakeを使用する場合は、.proを使用してください:
QT += qml scxml

次に、コンパイルするステートマシンを指定します:

STATECHARTS = ../trafficlight-common/statemachine.scxml
cmakeを使用する場合は、CMakeLists.txtを指定します:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
target_link_libraries(trafficlight-qml-static PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Qml
    Qt6::Scxml
)

次に、コンパイルするステートマシンを指定します:

qt6_add_statecharts(trafficlight-qml-static
    ../trafficlight-common/statemachine.scxml
)

statechartディレクティブSTATECHARTSまたはqt6_add_statechartsによりQt SCXML Compiler,qscxmlc 。自動的に実行され、statemachine.hと statemachine.cppが生成され、コンパイル用のヘッダーとソースとして適切に追加されます。

ステート・マシンのインスタンス化

生成されたTrafficLightStateMachine クラスをtrafficlight-widgets-static.cppファイルでインスタンス化する:

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

ステートへの接続

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

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