1
votes

In QML like follow:

Repeater {
    objectName: "main_layer_wnd"
    id: layer_show_list
    anchors.fill: parent
    model: delegateLayerModel
    delegate: delegateLayerShow
}

Component {
    id: delegateLayerShow
    LayerItemWndView {
        objectName: "delegateLayerShowItem"
    }
}

and in c++ code like follow:

mpMainLayerWnd = mpWnd->findChild<QQuickItem*>("main_layer_wnd");
QQuickItem* content = mpMainLayerWnd->property("contentItem").value<QQuickItem*>();
QList<QQuickItem*> list = content->childItems();

when run this code: content->childItems(); Here will throw an exception, can someone help me?

1

1 Answers

1
votes

So first something regarding your title: I have no idea what you consider the delegate data but maybe we will still find a satisfying answer.

For your error, I would say, it is expected. Let's break down your code into smaller parts:

mpMainLayerWnd = mpWnd->findChild<QQuickItem*>("main_layer_wnd"); // 1
QVariant prop = mpMainLayerWnd->property("contentItem")           // 2
QQuickItem* content = prop.value<QQuickItem*>();                  // 3
QList<QQuickItem*> list = content->childItems();                  // 4
  1. You find the child. This should be ok, and you'll have a QQuickItem* in mpMainLayerWnd
  2. You try to read the property contentItem that does not exist. You get an empty QVariant.
  3. You try to convert the content of the empty QVariant to a QQuickItem*, which will result in nullptr
  4. You try to access a method of a nullptr.

=> There is no method childItems() on a nullptr


What is unclear to my, why you expect to have contentItem in a Repeater, since non such is documented in QObject, QQuickItem nor in Repeater.

Most likely you mix up a default property with contentItem. The default property is the property to which QML automatically assigns everything you create within the { } of an object, without assigning it to some other property.

Item {
    id: rootItem
    Item { // this will go to the rootItems default property (data)
    }
}

The next thing is what I was wondering in the beginning: what is the delegate data you try to access? Do you want to get the Component? Then you can read the property delegate of the Repeater.

You want to get the instances of the delegate? Here I am a bit lost. I think they are in the children() of the Repeater, and in the childItem() of the Repeaters parent.