このページでは

SCXML 信号機(動的、QML)

動的に読み込まれるステートマシンを使用して信号機を実装した、Qt Quick アプリケーションです。

緑色のランプが点灯している信号機

「Traffic Light QML」は、動的に読み込まれるステートマシンの各ステートのアクティブなプロパティに接続する方法を示しています。

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

サンプルを実行する

このサンプルは、以下の手順で実行できます。

ステートマシンの動的読み込み

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

qmake を使用する場合の.proファイルへの記述:

QT += qml scxml

cmake を使用する場合のCMakeLists.txtへの記述:

find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
target_link_libraries(trafficlight-qml-dynamic PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Qml
    Qt6::Scxml
)

メインのQMLファイル内で、ステートマシンを動的に生成します:

import QtScxml
import TrafficLightApplication

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

    Lights {
        id: lights
        stateMachine: loader.stateMachine
        // Suppress qmllint warning, dynamic statemachine properties not known at compile-time
        // qmllint disable missing-property
        button.source: stateMachine.working ? "pause.png" : "play.png"
        button.onClicked: stateMachine.submitEvent(stateMachine.working ? "smash" : "repair");
        // qmllint enable missing-property
    }

    StateMachineLoader {
        id: loader
        source: Qt.resolvedUrl("statemachine.scxml")
    }
}

状態への接続

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: [
        // Suppress qmllint warning, dynamic statemachine properties not known at compile-time
        // qmllint disable missing-property
        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 }
        }
        // qmllint enable missing-property
    ]

サンプルプロジェクト @ code.qt.io

© 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.