Feu tricolore SCXML (dynamique, QML)
Une application Qt Quick qui utilise une machine à états chargée dynamiquement pour implémenter un feu tricolore.

Traffic Light QML montre comment se connecter aux propriétés actives d’un état dans une machine à états chargée dynamiquement.
L'interface utilisateur est créée à l'aide d'Qt Quick.
Exécution de l'exemple
Vous pouvez exécuter l'exemple à partir de :
- Qt Creator
Ouvrez le mode « Welcome » et sélectionnez l’exemple dans « Examples ». Pour plus d’informations, consultez Qt Creator: Tutoriel : Compiler et exécuter.
- Qt Extension for Visual Studio Code
Exécutez la commande « Qt: Open Qt examples » à partir de Command Palette, puis sélectionnez l’exemple dans la liste. Pour plus d’informations, consultez Qt Extension for Visual Studio Code: Tutoriel : Compiler et exécuter.
Chargement dynamique de la machine à états
Nous effectuons la liaison avec le module Qt SCXML en ajoutant les lignes suivantes aux fichiers de compilation de l'exemple.
Dans le fichier .pro lorsque vous utilisez qmake :
QT += qml scxmlDans le fichier CMakeLists.txt si vous utilisez cmake :
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
target_link_libraries(trafficlight-qml-dynamic PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Qml
Qt6::Scxml
)Nous créons dynamiquement la machine à états dans le fichier QML principal :
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")
}
}Connexion aux états
Dans le fichier SCXML, nous spécifions les états pour chaque feu : rouge, orange et vert. Dans l’élément ` <onentry> `, nous spécifions l’événement à envoyer lors de l’entrée dans l’état ainsi que le délai en secondes avant l’envoi de l’événement. Dans l’élément ` <transition> `, nous spécifions l’événement qui déclenche la transition vers l’état indiqué par l’attribut ` 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>Nous connectons les états comme suit :
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
]© 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.