QtScxml 状态机中的事件处理

Events handling in QtScxml state machine

我正在尝试使用 QScxmlStateMachine 对象,但不幸的是,当我的 transitioncond 属性被填充时,我无法触发事件,无论如何值。

machine.scxml:

<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" name="Machine" qt:editorversion="4.8.0" xmlns:qt="http://www.qt.io/2015/02/scxml-ext">
    <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="_event.data.dt === 'blah'"/>
    </state>
    <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"/>
    </state>
</scxml>

main.cpp:

#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;
    machine.start();

    connectToState(machine, "state1");
    connectToState(machine, "state2");

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

    return a.exec();
}

void displayActiveStates(Machine &machine) {
    for (auto state : machine.activeStateNames()) {
        qDebug(state.toLatin1());
    }
}

void connectToState(Machine &machine, const QString &state) {
    machine.connectToState(state, &machine, [&machine](bool active) {
        displayActiveStates(machine);
        qDebug(active ? "active" : "inactive");
    });
}

test_scxml.pro:

#-------------------------------------------------
#
# 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.
DEFINES += QT_DEPRECATED_WARNINGS

# 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 += \
        main.cpp

HEADERS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

STATECHARTS += \
    machine.scxml

当我删除这个属性时,一切正常。

有什么想法吗?

如果 docs 被审核:

Data Models

Qt SCXML supports the null data model, which must be supported by conforming SCXML processors, and the ECMAScript data model. In addition, Qt SCXML provices its own C++ data model that is implemented by the QScxmlCppDataModel class. The class enables writing C++ code for expr attributes and elements. The data part of the data model is backed by a subclass of QScxmlCppDataModel, for which the Qt SCXML compiler will generate the dispatch methods.

您需要添加 datamodel="ecmascript" 属性:

<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" name="Machine" qt:editorversion="4.8.0" xmlns:qt="http://www.qt.io/2015/02/scxml-ext" datamodel="ecmascript">
    <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="_event.data.dt === 'blah'"/>
    </state>
    <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"/>
    </state>
</scxml>