c++ - QtScxml 状态机中的事件处理

标签 c++ qt qt5 scxml

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

机器.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>

ma​​in.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>

关于c++ - QtScxml 状态机中的事件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53819265/

相关文章:

android - 显示半透明圆圈的快速动画

c++ - 使用 `std::conditional_t` 根据其模板参数定义类的 `typedef`

C++:不理解对象销毁规则

c++ - 在 netbeans IDE 中将 "media"文件添加到 c/c++ 项目的适当方法

c++ - CMake:将库链接到 KDE4 库时 undefined reference

qt - 使用 QPainter 的 drawText() 绘制时垂直居中文本

qt - 在 Qt 中,许多插槽连接到同一个信号,它们在发出信号时是否按顺序调用?

c++ - 如何在 CLion 中静态链接 Qt5 与 CMake?

c++ - GLSL 4.5 : Why is my "in" variable for Color Not Being Found by glGetAttribLocation

c++ - 你能把 std::recursive_mutex 和 std::condition_variable 结合起来吗?