【发布时间】:2017-02-01 14:18:33
【问题描述】:
我正在使用带有模型和委托的ListView。
模型是一个简单的ListModel,包含三个项目。每个项目都有一个值,键为myFirstRole。
委托包含一个Repeater 组件来创建任意数量的Labels。 Labels 必须使用模型中的数据。
中继器的型号不能设置为Listview的型号,因为我有Repeater使用其他数据。
这是我想要实现的一个最小示例:
//MyDelegate.qml
Component {
Item {
id: root
width: childrenRect.width
height childrenRect.height
Repeater {
model: 5 //It's not an option to set the repeaters model to the ListViews model. This example just illustrates my problem.
Label {
text: root.ListView.view.model.myFirstRole //This is the line where I want to be able to access the ListView's model, but I can't figure out how to properly reefer to it.
}
}
}
}
//MyListView.qml
ListView {
id: root
delegate: MyDelegate {}
model: ListModel {
ListElement {
myFirstRole: "one"
}
ListElement {
myFirstRole: "two"
}
ListElement {
myFirstRole: "three"
}
}
}
将 Qt 5.7.0 与 MSVC2015 32 位一起使用
【问题讨论】:
-
您的示例不完整。我根本看不到
ListView,但你在标题中提到了这一点。 -
@folibis:我认为没有必要,因为此示例的 ListView 代码只不过是标准样板 ListView 代码。为了完整起见,我可以添加它。
标签: qt listview qml qt5 repeater