Instantiating State Machines

Both the dynamically created and the compiled state machines behave in the same way, have the same properties, states, data model, and so on. They only differ in the way they are instantiated. To dynamically create one in C++ from an SCXML file, you can use:

auto *stateMachine = QScxmlStateMachine::fromFile("MyStatemachine.scxml");

Or, in QML:

import QtScxml 5.8

Item {
    property StateMachine stateMachine: scxmlLoader.stateMachine

    StateMachineLoader {
        id: scxmlLoader
        source: "statemachine.scxml"

A compiled state machine can be instantiated the same way as any C++ object:

auto *stateMachine = new MyStatemachine;


MyStatemachine stateMachine;

To use a compiled state machine in QML, you can register it as a QML type:

qmlRegisterType<MyStateMachine>("MyStateMachine", 1, 0, "MyStateMachine");

Then you can instantiate it in QML, like this:

import MyStateMachine 1.0

MyStateMachine {
    id: stateMachine

To compile a state machine, the following lines have to be added to a .pro file:

QT += scxml
STATECHARTS = MyStatemachine.scxml

This will tell qmake to run qscxmlc which generates MyStatemachine.h and MyStatemachine.cpp, and adds them to HEADERS and SOURCES variables. By default, the generated files are saved in the build directory. The QSCXMLC_DIR variable can be set to specify another directory. The QSCXMLC_NAMESPACE variable can be set to put the state machine code into a C++ namespace.

After instantiating a state machine, you can connect to any state's active property as follows. For example, if the state machine for a traffic light has a state indicating that the light is red (which has the convenient id "red" in the scxml file), you can write:

stateMachine->connectToState("red", [](bool active) {
    qDebug() << (active ? "entered" : "exited") << "the red state";

And in QML:

Light {
    id: greenLight
    color: "green"

If you want to be notified when a state machine sends out an event, you can connect to the corresponding signal. For example, for a media player state machine which indicates that playback has stopped by sending an event, you can write:

stateMachine->connectToEvent("playbackStopped", [](const QScxmlEvent &){
    qDebug() << "Stopped!";

And in QML:

import QtScxml 5.8

EventConnection {
    stateMachine: stateMachine
    events: ["playbackStopped"]
    onOccurred: console.log("Stopped!")

Sending events to a state machine is equally simple:

stateMachine->submitEvent("tap", QVariantMap({
    { "artist", "Fatboy Slim" },
    { "title", "The Rockafeller Skank" }

This will generate a "tap" event with the map contents available in inside the state machine. In QML:

stateMachine.submitEvent("tap", {
    "artist": "Fatboy Slim"
    "title": "The Rockafeller Skank"

Note: A state machine needs a QEventLoop to work correctly. The event loop is used to implement the delay attribute for events and to schedule the processing of a state machine when events are received from nested (or parent) state machines. A QML application or a widget application will always have an event loop running, so nothing extra is needed. For other applications, QApplication::run will have to be called to start the event loop processing.

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