12

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 to getData() method javascript function as parameter and call this function on C++ side?

So from QML point of view it should looks like:

action.getData(function(data) { alert(data); });

1 Answer 1

19

It is possible using QJSValue. For example:

//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)});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.