Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Fail to invoke StackView push method from c++

Fail to invoke StackView push method from c++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qtquickc++qmetaobjectinstackview
4 Posts 4 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oberluz
    wrote on 8 Jun 2016, 23:33 last edited by
    #1

    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

    M 1 Reply Last reply 9 Jun 2016, 18:34
    0
    • O oberluz
      8 Jun 2016, 23:33

      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

      M Offline
      M Offline
      medyakovvit
      wrote on 9 Jun 2016, 18:34 last edited by
      #2

      @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";
          }
      
      1 Reply Last reply
      0
      • A Offline
        A Offline
        achou
        wrote on 26 Aug 2016, 00:33 last edited by
        #3

        @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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jpnurmi
          wrote on 27 Aug 2016, 19:13 last edited by
          #4

          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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved