This question is deprecated
I have since converted my state machines to use an extra state to call the C++ function on enter and I trigger sendEvent
when the function returns a value.
Old question is as follows:
I have a simple SCXML model designed in Qt. It is lacking some functionality such as listening to pub/sub queues, for which I want to register some functions into the QScxmlStateMachine
instance so I can call them from the ECMAScript portion of the state machine. I took a look at the QScxmlEcmaScriptDataModel
, but I don't understand how I can modify the evaluate functions to register callbacks. Is it possible to register C++ functions and callbacks to Qt scxml state machines? If so, how?
Additional information:
I created a class called RegisterJs
, this is registerjs.h
#ifndef REGISTERJS_H
#define REGISTERJS_H
#include <QObject>
#include <QString>
#include <QQmlEngine>
class RegisterJS : public QObject
{
Q_OBJECT
public:
explicit RegisterJS(QObject *parent = nullptr);
Q_INVOKABLE void showMessage(QString message);
signals:
public slots:
};
static QObject *registerJSProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
RegisterJS *singletonClass = new RegisterJS();
return singletonClass;
}
#endif // REGISTERJS_H
This is registerjs.cpp
#include "registerjs.h"
#include <QMessageBox>
RegisterJS::RegisterJS(QObject *parent) : QObject(parent)
{
}
void RegisterJS::showMessage(QString message){
QMessageBox::information(0, message, message);
}
This is how I register the class:
qmlRegisterSingletonType<RegisterJS>("in.nurett", 1, 0, "RegisterJS", registerJSProvider);
This is the content of testflow.js:
function testShowMessage() { RegisterJS.showMessage("test"); }
This is the relevant content from testflow.scxml:
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" xmlns:qt="http://www.qt.io/2015/02/scxml-ext" name="teststate" qt:editorversion="4.6.2" initial="initialize">
<state id="initialize">
<onentry>
<script>testShowMessage();</script>
</onentry>
</state>
<script src=":testflow.js"/>
But the information message doesn't show up.