SCXML 계산기

SCXML 사양에 제시된 계산기 예제를 구현하는 애플리케이션입니다.

Calculator는 Qt SCXML 을 사용하여 SCXML 사양에 제시된 계산기 예제를 구현합니다.

상태 머신은 statemachine.scxml 파일에 지정되어 있으며 CalculatorStateMachine 클래스로 컴파일됩니다. 사용자 인터페이스는 Qt Quick 를 사용하여 생성됩니다.

예제 실행하기

에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.

상태 머신 인스턴스화하기

생성된 CalculatorStateMachine 클래스를 계산기-qml.h 파일에서 QML 유형으로 선언하여 QML에서 사용할 수 있도록 합니다:

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

계산기 스테이트 머신을 인스턴스화하고 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.