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 libA.so: (null):0 ((null)): QMetaObject::invokeMethod: No such method QQuickStackView::push(QVariant)
    W libA.so: Candidates are:
    W lib.so:     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?

    Thanks



  • @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
        if(stackView)
        {
            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:
    push(QQmlV4Function*)

    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.

    Thanks



  • 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.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.