SCXML 信号機(シンプル版、QML)
コンパイルされたステートマシンを使用して、簡略化された信号機を実装したQt Quick アプリケーションです。

「Traffic Light」は、クラスにコンパイルされたステートマシンにおいて、ステートのアクティブなプロパティに接続する方法を示しています。
UIはQt Quick を使用して作成されています。
サンプルを実行する
このサンプルは、以下の手順で実行できます。
- 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 += qml scxml次に、コンパイルするステートマシンを指定します:
STATECHARTS = statemachine.scxmlcmake を使用する場合の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 {
id: stateMachine
running: true
}状態への接続
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>各状態への接続は次のように行います:
Light {
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
color: "red"
visible: stateMachine.red || stateMachine.redGoingGreen
}
Light {
anchors.centerIn: parent
color: "yellow"
visible: stateMachine.yellow || stateMachine.blinking
}
Light {
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
color: "green"
visible: stateMachine.green
}
}© 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.