Solved 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 thecond
attribute of mytransition
is filled, whatever the value.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="true"/> </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"); 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
When I remove this attribute, everything works fine.
Any idea?
-
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="_event.data.dt === "blah""/>
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 ;)