Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to access QML Repeater delegate items in C++?
QtWS25 Last Chance

How to access QML Repeater delegate items in C++?

Scheduled Pinned Locked Moved Solved General and Desktop
qmlrepeater
4 Posts 3 Posters 1.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.
  • R Offline
    R Offline
    RAJEESHR
    wrote on 16 Sept 2020, 12:56 last edited by RAJEESHR
    #1

    //// C++

    void MyViewModel::doAccessRepeater()
    {
        QObject* rootObject = QQmlApplicationEngine_->rootObjects().first();
        QObject* repeater = rootObject->findChild<QObject*>("SelectionButtonRepeater"); /// Returned a valid pointer
        QObject* item0 = repeater->findChild<QObject*>("SelectionViewRow0"); /// returns null
        ...
    }
    

    /// QML

    Repeater {
                objectName: "SelectionButtonRepeater"
                model: (viewModel === null) ? "" : viewModel.list
                Text {
                    width: parent.width
                    height: 40
                    objectName: ("SelectionViewRow" + index)
                    text: modelData
                }
    }
    
    1 Reply Last reply
    0
    • F fcarney
      16 Sept 2020, 14:22

      Use the signals for adding and removing items from the repeater:
      https://doc.qt.io/qt-5/qml-qtquick-repeater.html#signals
      Have it add those to a list in C++ via the Connections object.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 16 Sept 2020, 20:47 last edited by raven-worx
      #3

      @fcarney
      via the itemAt() method

      // int count = QQmlProperty::read(repeaterObj, "count").toInt();
      QQuickItem* item;
      int index = 0;
      QMetaObject::invokeMethod(repeaterObj, "itemAt", Qt::DirectConnection,
                                Q_RETURN_ARG(QQuickItem*, item),
                                Q_ARG(int, index));
      

      If it throws a warning/error for QQuickItem* in the Q_RETURN_ARG, try with QObject* instead and cast it to a QQuickItem pointer using qobject_cast<QQuickItem*>(item)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      R 1 Reply Last reply 4 Oct 2020, 10:22
      3
      • F Offline
        F Offline
        fcarney
        wrote on 16 Sept 2020, 14:22 last edited by
        #2

        Use the signals for adding and removing items from the repeater:
        https://doc.qt.io/qt-5/qml-qtquick-repeater.html#signals
        Have it add those to a list in C++ via the Connections object.

        C++ is a perfectly valid school of magic.

        R 1 Reply Last reply 16 Sept 2020, 20:47
        0
        • F fcarney
          16 Sept 2020, 14:22

          Use the signals for adding and removing items from the repeater:
          https://doc.qt.io/qt-5/qml-qtquick-repeater.html#signals
          Have it add those to a list in C++ via the Connections object.

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 16 Sept 2020, 20:47 last edited by raven-worx
          #3

          @fcarney
          via the itemAt() method

          // int count = QQmlProperty::read(repeaterObj, "count").toInt();
          QQuickItem* item;
          int index = 0;
          QMetaObject::invokeMethod(repeaterObj, "itemAt", Qt::DirectConnection,
                                    Q_RETURN_ARG(QQuickItem*, item),
                                    Q_ARG(int, index));
          

          If it throws a warning/error for QQuickItem* in the Q_RETURN_ARG, try with QObject* instead and cast it to a QQuickItem pointer using qobject_cast<QQuickItem*>(item)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          R 1 Reply Last reply 4 Oct 2020, 10:22
          3
          • R raven-worx
            16 Sept 2020, 20:47

            @fcarney
            via the itemAt() method

            // int count = QQmlProperty::read(repeaterObj, "count").toInt();
            QQuickItem* item;
            int index = 0;
            QMetaObject::invokeMethod(repeaterObj, "itemAt", Qt::DirectConnection,
                                      Q_RETURN_ARG(QQuickItem*, item),
                                      Q_ARG(int, index));
            

            If it throws a warning/error for QQuickItem* in the Q_RETURN_ARG, try with QObject* instead and cast it to a QQuickItem pointer using qobject_cast<QQuickItem*>(item)

            R Offline
            R Offline
            RAJEESHR
            wrote on 4 Oct 2020, 10:22 last edited by
            #4

            @raven-worx thanks. it works.!
            Thank you all for ur supports.

            1 Reply Last reply
            0

            • Login

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