このページでは

SCXML 電卓

SCXML仕様書に記載されている「Calculator」のサンプルを実装したアプリケーションです。

数字表示、演算ボタン、および数字ボタンを備えた電卓

Calculatorは、Qt SCXML を使用して、SCXML仕様書に記載されている「Calculator」のサンプルを実装しています。

ステートマシンはstatemachine.scxmlファイルで指定され、CalculatorStateMachine クラスにコンパイルされます。ユーザーインターフェースは、Qt Quick を使用して作成されます。

例の動作

以下の手順でサンプルを実行できます。

ステートマシンのインスタンス化

生成されたCalculatorStateMachine クラスをcalculator-qml.hファイル内で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.