Important: Please read the Qt Code of Conduct -

Fail to invoke StackView push method from c++

  • Hi,

    I'm having an isssue invoking the push() method on a QML StackView from C++. The code is:

        QVariant arg("qrc:/Common/whatever.qml");
        bool ok = QMetaObject::invokeMethod(m_stackView, "push", Q_ARG(QVariant, arg));

    At runtime it complains:

    W (null):0 ((null)): QMetaObject::invokeMethod: No such method QQuickStackView::push(QVariant)
    W Candidates are:
    W     push(QQmlV4Function*)

    I am confused because the docs state that:

    'Notice the Q_RETURN_ARG() and Q_ARG() arguments for QMetaObject::invokeMethod() must be specified as QVariant types, as this is the generic data type used for QML method parameters and return values.'

    The m_stackView is a QObject* and seems correct because the error refers to a QQuickStackView.

    Any ideas what I'm doing wrong?


  • @oberluz
    Well, StackView::push() has argument of type Item. But you try to send

    QVariant arg("qrc:/Common/whatever.qml");

    I think, in qml it's converted to string, not an item which must to be loaded from mentioned path. So, i think, you must to load some component in c++, to use QVariant::fromValue() to create QVariant and send it to StackView::push(). For example:

    QObject *stackView = engine.rootObjects().at(0)->findChild<QObject*>("myStackView"); // search element by "objectName" property
            QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/StackViewMethodFromCpp.qml")));
            QObject *object = component.create();
            QVariant arg = QVariant::fromValue(object);
            if(!QMetaObject::invokeMethod(stackView, "push",
                                          Q_ARG(QVariant, arg)))
                qDebug() << "Failed to invoke push";

  • @oberluz did this solution work for you? I'm running into the same issue and when I tried @medyakovvit 's solution I still got the same error:

    QMetaObject::invokeMethod: No such method QQuickStackView::push(QVariant)
    Candidates are:

    I also tried to use a simple QUrl since the documentation says it can be an item, component, or url, but that led to the same error.


  • Why are you guys trying to invoke StackView methods directly from C++? StackView has been designed to be used from QML. Probably best if you don't let your C++ backend even know anything about the stack view. You can, for example, just emit signals from the C++ backend, and make the QML UI react to that and do the actual push. This is the normal way to communicate between C++ backends and QML UI layers.