SCXML 计算器

实现 SCXML 规范中的计算器示例的应用程序。

计算器使用Qt SCXML 实现 SCXML 规范中的计算器示例

状态机在statemachine.scxml文件中指定,并编译到CalculatorStateMachine 类中。用户界面通过Qt Quick 创建。

运行示例

运行示例 Qt Creator,打开Welcome 模式,并从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建并运行

实例化状态机

我们在calculator-qml.h文件中将生成的CalculatorStateMachine 类声明为 QML 类型,使其在 QML 中可用:

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

我们实例化 CalculatorStateMachine 并监听updateDisplay 事件。当事件发生时,我们会更改计算器显示屏上的文本:

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

当用户按下计算器按钮时,按钮会向状态机提交事件:

        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")
        }

示例项目 @ code.qt.io

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