Qt SCXML Calculator QML Example

../_images/calculator-qml.png

A Qt Quick application that implements the Calculator example presented in the SCXML Specification.

Calculator uses Qt SCXML to implement the Calculator Example presented in the SCXML Specification.

The state machine is specified in the statemachine.scxml file and compiled into the CalculatorStateMachine class. The user interface is created using Qt Quick.

Running the Example

To run the example from Qt Creator , open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Compiling the State Machine

We link against the Qt SCXML module by adding the following lines to the example’s build files.

  • .pro*

when using qmake:

We then specify the state machine to compile:

  • CMakeLists.txt*

when using cmake:

We then specify the state machine to compile:

The statechart directives STATECHARTS or qt6_add_statecharts invoke the Qt SCXML Compiler, qscxmlc, which is run automatically to generate statemachine.h and statemachine.cpp, which are then added appropriately as headers and sources for compilation.

Instantiating the State Machine

We make the generated CalculatorStateMachine class available to QML by registering it as a QML type in the calculator-qml.cpp file:

To use the CalculatorStateMachine type in a QML file, we import it:

We instantiate a CalculatorStateMachine and listen to the updateDisplay event. When it occurs, we change the text on the calculator display:

When users press the calculator buttons, the buttons submit events to the state machine:

Example project @ code.qt.io