SCXML 信号機 (シンプル、QML)
Qt Quick アプリケーションです。コンパイルされたステートマシンを使って、簡略化された信号機を実装しています。
Traffic Lightは、クラスにコンパイルされたステートマシンの状態のアクティブプロパティに接続する方法を示します。
UI はQt Quick を使用して作成されています。
例の実行
からサンプルを実行するには 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 { 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 } }
© 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.