Passing a Java Script function as a parameter to a C ++ function

I declare my object in C ++

class Action : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name) public: Action(): QObject(0) {} QString name() const { return "rem"; } Q_INVOKABLE void getData() {}; } 

and make it available to qml:

 engine()->rootContext()->setContextProperty("action", new Action()); 

How to pass getData () javascript method as parameter and call this function on C ++ side?

So, from a QML perspective, it should look like this:

 action.getData(function(data) { alert(data); }); 
+6
source share
1 answer

Possible to use QJSValue . For instance:

 //C++ Q_INVOKABLE void getData(QJSValue value) { if (value.isCallable()) { QJSValueList args; args << QJSValue("Hello, world!"); value.call(args); } } //QML action.getData(function (data){console.log(data)}); 
+11
source

Source: https://habr.com/ru/post/984702/


All Articles