이 페이지에서

SCXML 계산기

SCXML 사양서에 제시된 ‘Calculator’ 예제를 구현한 애플리케이션입니다.

숫자 표시창, 연산 버튼 및 숫자 버튼이 있는 계산기

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

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

예제 실행

다음 경로에서 예제를 실행할 수 있습니다:

상태 머신 인스턴스화

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

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