Sur cette page

Feu de circulation SCXML (dynamique, widgets)

Une application basée sur un widget qui utilise une machine à états chargée dynamiquement pour mettre en œuvre un feu de circulation.

Feu de circulation avec feu vert allumé

Traffic Light 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 de Qt Widgets.

Exécution de l'exemple

Pour exécuter l'exemple à partir de Qt CreatorOuvrez le mode Welcome et sélectionnez l'exemple à partir de Examples. Pour plus d'informations, voir Qt Creator: Tutorial : Construire et exécuter.

Chargement dynamique de la machine à états

Nous établissons un lien avec le module Qt SCXML en ajoutant les lignes suivantes aux fichiers de compilation de l'exemple.

Au fichier .pro si vous utilisez qmake :

QT += widgets scxml

À CMakeLists.txt si vous utilisez cmake :

find_package(Qt6 REQUIRED COMPONENTS Core Gui Scxml Widgets)

target_link_libraries(trafficlight-widgets-dynamic PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Scxml
    Qt6::Widgets
)

Nous créons dynamiquement la machine à états dans trafficlight-widgets-dynamic.cpp:

#include "../trafficlight-common/trafficlight.h"

#include <QtWidgets/qapplication.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qtextstream.h>

using namespace Qt::Literals::StringLiterals;

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QScxmlStateMachine *machine = QScxmlStateMachine::fromFile(u":statemachine.scxml"_s);

    if (!machine->parseErrors().isEmpty()) {
        QTextStream errs(stderr, QIODevice::WriteOnly);
        const auto errors = machine->parseErrors();
        for (const QScxmlError &error : errors) {
            errs << error.toString();
        }
        return -1;
    }

Puis nous l'instancions :

    TrafficLight widget(machine);
    widget.show();
    machine->setParent(&widget);
    machine->start();

    return app.exec();
}

Connexion aux états

Dans le fichier SCXML, nous spécifions les états de chaque feu : rouge, jaune et vert. Dans l'élément <onentry>, nous spécifions l'événement à envoyer lors de l'entrée dans l'état et 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 spécifié 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 nous connectons aux états comme suit :

    machine->connectToState(u"red"_s, widget->redLight(), &LightWidget::switchLight);
    machine->connectToState(u"redGoingGreen"_s, widget->redLight(), &LightWidget::switchLight);
    machine->connectToState(u"yellow"_s, widget->yellowLight(), &LightWidget::switchLight);
    machine->connectToState(u"blinking"_s, widget->yellowLight(), &LightWidget::switchLight);
    machine->connectToState(u"green"_s, widget->greenLight(), &LightWidget::switchLight);

Exemple de projet @ 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.