Important: Please read the Qt Code of Conduct -

Events handling in QtScxml state machine

  • I'm trying to use the QScxmlStateMachine object, but unfortunately, I don't manage to trigger an event when the cond attribute of my transition is filled, whatever the value.


    <?xml version="1.0" encoding="UTF-8"?>
    <scxml xmlns="" version="1.0" binding="early" name="Machine" qt:editorversion="4.8.0" xmlns:qt="">
        <state id="state1">
            <qt:editorinfo scenegeometry="351.24;371.78;291.24;321.78;120;100" geometry="351.24;371.78;-60;-50;120;100"/>
            <transition type="external" event="event1" target="state2" cond="true"/>
        <state id="state2">
            <qt:editorinfo scenegeometry="614.16;371.78;554.16;321.78;120;100" geometry="614.16;371.78;-60;-50;120;100"/>


    #include <QApplication>
    #include "machine.h"
    using namespace std;
    void displayActiveStates(Machine &machine);
    void connectToState(Machine &machine, const QString &state);
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        Machine machine;
        connectToState(machine, "state1");
        connectToState(machine, "state2");
        return a.exec();
    void displayActiveStates(Machine &machine) {
        for (auto state : machine.activeStateNames()) {
    void connectToState(Machine &machine, const QString &state) {
        machine.connectToState(state, &machine, [&machine](bool active) {
            qDebug(active ? "active" : "inactive");

    # Project created by QtCreator 2018-12-17T16:28:49
    QT       += core gui scxml
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = test_scxml
    TEMPLATE = app
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    CONFIG += c++11
    SOURCES += \
    HEADERS +=
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target

    When I remove this attribute, everything works fine.

    Any idea?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Might be a silly question but did you already looked at the QtScxml examples ?

  • Thank you.
    Yes, I did, but I found none of them matching my issue.
    Maybe the expression passed as condition was not correct, bu I tried many things, without success.
    For example, I tried to pass parameters to the event like that:

    machine.submitEvent("event1", QVariantMap({
         {"dt", "blah"}

    And, as a condtion for the transition in my scxml file, I set:

    <transition type="external" event="event1" target="state2" cond=" === &quot;blah&quot;"/>

    But the event does not seem to trigger.
    (Sorry for my bad english)

  • I forgot to add datamodel="ecmascript" to the scxml file.
    Problem solved! Thank you ;)

Log in to reply