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が生成され、コンパイル用のヘッダーとソースとして適切に追加されます。

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

ステート・マシンのインスタンス化は以下のように行う:

import TrafficLightApplication

Window {
    width: lights.width
    height: lights.height
    visible: true

    Lights {
        id: lights
        stateMachine: TrafficLightStateMachine {
            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>

ステートへの接続は以下のように行う:

    states: [
        State {
            name: "Red"
            when: lights.stateMachine.red

            PropertyChanges { redLight.opacity: 1 }
        },
        State {
            name: "RedGoingGreen"
            when: lights.stateMachine.redGoingGreen

            PropertyChanges { redLight.opacity: 1 }
            PropertyChanges { yellowLight.opacity: 1 }
        },
        State {
            name: "Yellow"
            when: lights.stateMachine.yellow || lights.stateMachine.blinking

            PropertyChanges { yellowLight.opacity: 1 }
        },
        State {
            name: "Green"
            when: lights.stateMachine.green

            PropertyChanges { greenLight.opacity: 1 }
        }
    ]

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