本页内容

SCXML 交通信号灯(静态,QML)

一个使用编译后的状态机来实现交通信号灯的Qt Quick 应用程序。

交通信号灯亮起绿灯

“交通信号灯”演示了如何连接到已编译为类的状态机中某个状态的活动属性。

用户界面使用Qt Quick 创建。

运行示例

您可以通过以下方式运行该示例:

编译状态机

通过在示例的构建文件中添加以下几行代码,将Qt SCXML 模块链接进来。

使用 qmake 时,在.pro文件中:
QT += qml scxml

然后指定要编译的状态机:

STATECHARTS = statemachine.scxml
使用 cmake 时,在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 文件,随后这些文件将作为头文件和源文件被恰当地添加到编译过程中。

实例化状态机

我们按以下方式实例化状态机:

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

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