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

Calculatorは、Qt SCXML を使用して、SCXML仕様書に記載されている「Calculator」のサンプルを実装しています。
ステートマシンはstatemachine.scxmlファイルで指定され、CalculatorStateMachine クラスにコンパイルされます。ユーザーインターフェースは、Qt Quick を使用して作成されます。
例の動作
以下の手順でサンプルを実行できます。
- Qt Creator
Welcome モードを開き、Examples からこの例を選択します。詳細については、Qt Creator の「チュートリアル:ビルドと実行」を参照してください。
- Qt Extension for Visual Studio Code
Command Palette から `Qt: Open Qt examples ` コマンドを実行し、リストからサンプルを選択します。詳細については、Qt Extension for Visual Studio Code: チュートリアル「ビルドと実行」を参照してください。
ステートマシンのインスタンス化
生成された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")
}© 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.