SCXML 信号機(動的、ウィジェット)
動的に読み込まれるステートマシンを使用して信号機を実装する、ウィジェットベースのアプリケーションです。

「Traffic Light」は、動的に読み込まれるステートマシンのステートのアクティブなプロパティに接続する方法を示しています。
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.