Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • //// 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
                }
    }
    

  • Moderators

    @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)



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


  • Moderators

    @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)



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


Log in to reply