如何在 C++ 中获取 Repeater 的委托数据?
how to get Repeater's delegate data in c++?
在 QML 中如下所示:
Repeater {
objectName: "main_layer_wnd"
id: layer_show_list
anchors.fill: parent
model: delegateLayerModel
delegate: delegateLayerShow
}
Component {
id: delegateLayerShow
LayerItemWndView {
objectName: "delegateLayerShowItem"
}
}
在 C++ 代码中,如下所示:
mpMainLayerWnd = mpWnd->findChild<QQuickItem*>("main_layer_wnd");
QQuickItem* content = mpMainLayerWnd->property("contentItem").value<QQuickItem*>();
QList<QQuickItem*> list = content->childItems();
当 运行 此代码时:
内容->childItems();
这里会抛出异常,有人能帮帮我吗?
首先关于您的头衔:我不知道您如何看待代表数据,但也许我们仍会找到令人满意的答案。
对于你的错误,我想说,这是意料之中的。让我们将您的代码分解成更小的部分:
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
- 您找到了child。这应该没问题,您将在 mpMainLayerWnd
中有一个 QQuickItem*
- 您尝试读取不存在的属性
contentItem
。你得到一个空的 QVariant
.
- 您尝试将空
QVariant
的内容转换为 QQuickItem*
,这将导致 nullptr
- 您尝试访问
nullptr
的方法。
=> nullptr
上没有方法 childItems()
我不清楚的是,为什么您希望 Repeater
中有 contentItem
,因为 QObject
、QQuickItem
和 [=] 中都没有记录22=].
您很可能混淆了 default property
和 contentItem
。默认的 属性 是 属性,QML 会自动将您在 object 的 { }
中创建的所有内容分配给它,而不会将其分配给其他 属性。
Item {
id: rootItem
Item { // this will go to the rootItems default property (data)
}
}
接下来就是我一开始想知道的事情:您尝试访问的委托数据是什么?你想得到组件吗?然后就可以阅读属性delegate
的Repeater
.
您想获取 delegate
的实例吗?在这里我有点迷路了。我认为它们在 Repeater
的 children()
和 Repeater
的 childItem()
parent.
在 QML 中如下所示:
Repeater {
objectName: "main_layer_wnd"
id: layer_show_list
anchors.fill: parent
model: delegateLayerModel
delegate: delegateLayerShow
}
Component {
id: delegateLayerShow
LayerItemWndView {
objectName: "delegateLayerShowItem"
}
}
在 C++ 代码中,如下所示:
mpMainLayerWnd = mpWnd->findChild<QQuickItem*>("main_layer_wnd");
QQuickItem* content = mpMainLayerWnd->property("contentItem").value<QQuickItem*>();
QList<QQuickItem*> list = content->childItems();
当 运行 此代码时: 内容->childItems(); 这里会抛出异常,有人能帮帮我吗?
首先关于您的头衔:我不知道您如何看待代表数据,但也许我们仍会找到令人满意的答案。
对于你的错误,我想说,这是意料之中的。让我们将您的代码分解成更小的部分:
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
- 您找到了child。这应该没问题,您将在 mpMainLayerWnd 中有一个
- 您尝试读取不存在的属性
contentItem
。你得到一个空的QVariant
. - 您尝试将空
QVariant
的内容转换为QQuickItem*
,这将导致nullptr
- 您尝试访问
nullptr
的方法。
QQuickItem*
=> nullptr
childItems()
我不清楚的是,为什么您希望 Repeater
中有 contentItem
,因为 QObject
、QQuickItem
和 [=] 中都没有记录22=].
您很可能混淆了 default property
和 contentItem
。默认的 属性 是 属性,QML 会自动将您在 object 的 { }
中创建的所有内容分配给它,而不会将其分配给其他 属性。
Item {
id: rootItem
Item { // this will go to the rootItems default property (data)
}
}
接下来就是我一开始想知道的事情:您尝试访问的委托数据是什么?你想得到组件吗?然后就可以阅读属性delegate
的Repeater
.
您想获取 delegate
的实例吗?在这里我有点迷路了。我认为它们在 Repeater
的 children()
和 Repeater
的 childItem()
parent.