In questa pagina

Semaforo SCXML (dinamico, widget)

Un'applicazione basata su widget che utilizza una macchina a stati caricata dinamicamente per implementare un semaforo.

Semaforo con la luce verde accesa

Il semaforo mostra come collegarsi alle proprietà attive di uno stato in una macchina a stati caricata dinamicamente.

L'interfaccia utente è stata creata utilizzando Qt Widgets.

Esecuzione dell’esempio

È possibile eseguire l'esempio da:

Caricamento dinamico della macchina a stati

Si effettua il collegamento al modulo Qt SCXML aggiungendo le seguenti righe ai file di compilazione dell’esempio.

Nel file .pro quando si utilizza qmake:

QT += widgets scxml

Nel file CMakeLists.txt quando si utilizza 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
)

Creiamo dinamicamente la macchina a stati in trafficlight-widgets-dynamic.cpp:

#include "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;
    }

E poi la istanziamo:

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

    return app.exec();
}

Collegamento agli stati

Nel file SCXML, specifichiamo gli stati per ciascun semaforo: rosso, giallo e verde. Nell'elemento <onentry>, specifichiamo l'evento da inviare all'entrata nello stato e il ritardo in secondi prima dell'invio dell'evento. Nell'elemento <transition>, specifichiamo l'evento che innesca la transizione allo stato specificato dall'attributo 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>

Colleghiamo gli stati come segue:

    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);

Progetto di esempio su @ 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.