In questa pagina

Calcolatrice SCXML

Un'applicazione che implementa l'esempio "Calcolatrice" presentato nella specifica SCXML.

Calcolatrice con display numerico, tasti operatori e tasti numerici

Calculator utilizza Qt SCXML per implementare l’esempio "Calculator " presentato nella specifica SCXML.

La macchina a stati è specificata nel file statemachine.scxml e compilata nella classe CalculatorStateMachine. L’interfaccia utente viene creata utilizzando Qt Quick.

Esecuzione dell’esempio

È possibile eseguire l’esempio da:

Istanziamento della macchina a stati

Rendiamo la classe CalculatorStateMachine generata disponibile per QML dichiarandola come tipo QML nel file calculator-qml.h:

struct CalculatorStateMachineRegistration
{
    Q_GADGET
    QML_FOREIGN(CalculatorStateMachine)
    QML_NAMED_ELEMENT(CalculatorStateMachine)
    QML_ADDED_IN_VERSION(1, 0)
};

Istanziamo una CalculatorStateMachine e ascoltiamo l’evento updateDisplay. Quando si verifica, modifichiamo il testo sul display della calcolatrice:

    CalculatorStateMachine {
        id: statemachine
        running: true
        EventConnection {
            events: ["updateDisplay"]
            onOccurred: (event)=> resultText.text = event.data.display
        }
    }

Quando gli utenti premono i tasti della calcolatrice, questi inviano degli eventi alla macchina a stati:

        Button {
            id: resultButton
            x: 3 * width
            y: parent.height / 5
            textHeight: y - 2
            fontHeight: 0.4
            width: parent.width / 4
            height: y * 4
            color: pressed ? "#e0b91c" : "#face20"
            text: "="
            onClicked: statemachine.submitEvent("EQUALS")
        }

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.