SCXML 信号機 (動的、ウィジェット)

動的にロードされるステートマシンを使用して信号機を実装する、ウィジェットベースのアプリケーションです。

トラフィックライトは、動的にロードされるステートマシンの状態のアクティブプロパティに接続する方法を示します。

UI はQt Widgets を使用して作成されています。

例の実行

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

ステートマシンの動的ロード

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

qmakeを使用する場合は.proに

QT += widgets scxml

cmakeを使用する場合は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-common/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);

プロジェクト例 @ 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.