Congratulations to our 2022 Qt Champions!

Pass Java Script function to C++ method

  • I declare my object in C++

    class Action : public QObject
      Q_PROPERTY(QString name READ name)
      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); });

  • I found solution!

    Q_INVOKABLE void getCount(QJSValue function)
    { << "qqww");

Log in to reply