How to access QML Repeater delegate items in C++?
Solved
General and Desktop
-
//// 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 } }
-
@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 withQObject*
instead and cast it to aQQuickItem
pointer usingqobject_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. -
@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 withQObject*
instead and cast it to aQQuickItem
pointer usingqobject_cast<QQuickItem*>(item)
-
@raven-worx thanks. it works.!
Thank you all for ur supports.