在委托中的 Repeater 组件中访问 ListView 模型
Access ListView model within the Repeater component in the delegate
我正在使用 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 位
//MyDelegate.qml
Item {
id: root
property var listViewModel // pass the model data to here
width: 100
height: 50
Column {
Repeater {
model: 5 // Use a different model here
Text {
width: 50
height: 10
text: listViewModel.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
width: 100
height: 500
delegate: MyDelegate {
listViewModel: model // set the model data here
}
model: ListModel {
ListElement {
myFirstRole: "one"
}
ListElement {
myFirstRole: "two"
}
ListElement {
myFirstRole: "three"
}
}
}
查看代码中的注释。猜测您想要达到的目标并非易事,但我希望我猜对了。
我认为您无法通过 here 中提到的特殊 model
属性 访问角色(我假设您正在尝试这样做)来自Repeater
的范围。相反,您可以在组件的根级别声明一个 属性,然后可以在嵌套范围中使用它:
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
ListView {
anchors.fill: parent
model: ListModel {
ListElement { myFirstRole: "Dog" }
ListElement { myFirstRole: "Cat" }
}
delegate: Item {
id: root
width: childrenRect.width
height: childrenRect.height
property string myFirstRoleData: myFirstRole
Repeater {
model: 5
Text {
text: myFirstRoleData
}
}
}
}
}
如果您有很多属性,这可能会有点乏味。通过一些快速的尝试,看起来也可以将整个 model
对象存储在 属性:
中
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
ListView {
anchors.fill: parent
model: ListModel {
ListElement { myFirstRole: "Dog" }
ListElement { myFirstRole: "Cat" }
}
delegate: Item {
id: root
width: childrenRect.width
height: childrenRect.height
property var modelData: model
Repeater {
model: 5
Text {
text: root.modelData.myFirstRole
}
}
}
}
}
modelData
可能不是最好的名称,因为 Qt 将该名称用于只有一个角色的模型,但是......如果你采用这种方法,你会无论如何都有不止一个角色。 :)
看起来像 Qt Quick Controls' (1) Tumbler
does this too.
我正在使用 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 位
//MyDelegate.qml
Item {
id: root
property var listViewModel // pass the model data to here
width: 100
height: 50
Column {
Repeater {
model: 5 // Use a different model here
Text {
width: 50
height: 10
text: listViewModel.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
width: 100
height: 500
delegate: MyDelegate {
listViewModel: model // set the model data here
}
model: ListModel {
ListElement {
myFirstRole: "one"
}
ListElement {
myFirstRole: "two"
}
ListElement {
myFirstRole: "three"
}
}
}
查看代码中的注释。猜测您想要达到的目标并非易事,但我希望我猜对了。
我认为您无法通过 here 中提到的特殊 model
属性 访问角色(我假设您正在尝试这样做)来自Repeater
的范围。相反,您可以在组件的根级别声明一个 属性,然后可以在嵌套范围中使用它:
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
ListView {
anchors.fill: parent
model: ListModel {
ListElement { myFirstRole: "Dog" }
ListElement { myFirstRole: "Cat" }
}
delegate: Item {
id: root
width: childrenRect.width
height: childrenRect.height
property string myFirstRoleData: myFirstRole
Repeater {
model: 5
Text {
text: myFirstRoleData
}
}
}
}
}
如果您有很多属性,这可能会有点乏味。通过一些快速的尝试,看起来也可以将整个 model
对象存储在 属性:
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
ListView {
anchors.fill: parent
model: ListModel {
ListElement { myFirstRole: "Dog" }
ListElement { myFirstRole: "Cat" }
}
delegate: Item {
id: root
width: childrenRect.width
height: childrenRect.height
property var modelData: model
Repeater {
model: 5
Text {
text: root.modelData.myFirstRole
}
}
}
}
}
modelData
可能不是最好的名称,因为 Qt 将该名称用于只有一个角色的模型,但是......如果你采用这种方法,你会无论如何都有不止一个角色。 :)
看起来像 Qt Quick Controls' (1) Tumbler
does this too.