根据 ListModel 计数动态创建页面
Dynamically create page based on ListModel count
我是初学者QT/QML应用程序开发
如何根据 ListModel 计数动态创建 qml。
在视图中,我使用 Repeater 在 GridLayout 中列出了模型对象。
Item{
id:griditem
anchors.fill:parent
GridLayout{
id: grid2
x:145
y:30
Layout.preferredHeight: 480
Layout.preferredWidth: 1135
rowSpacing:10
columnSpacing:40
columns: 3
rows: 2
Repeater{
id: repeater_Id
model: FeatureModel{}
Loader{
id: loader_Id
source: "QuadTiles.qml"
onLoaded: {
loader_Id.item.nIndex=index
loader_Id.item.type_String = type
loader_Id.item.title_Text.text = title
loader_Id.item.description_Text.text = description
loader_Id.item.btn1_icon.source = icon1
}
}
} //Repeater
}//GridLayout
}
编辑:
我面临一些问题
我需要根据 ModelList 计数动态创建新视图。 GridLayout
中每页最多有 6 个项目(3 行和 2 列)
'QuadTiles.qml'是加载到GridLayout的每一项的qml文件
尝试这样的事情:
lm
是要拆分的 ListModel
。
SwipeView {
width: 200
height: 800
clip: true
currentIndex: 0
Repeater {
model: Math.ceil(lm.count / 6)
delegate: ListView {
width: 200
height: 800
property int viewIndex: index
model: DelegateModel {
model: lm
groups: DelegateModelGroup { name: 'filter' }
Component.onCompleted: {
for (var i = viewIndex * 6; i < lm.count && i < (viewIndex * 6) + 6; i++) {
items.setGroups(i, 1, ['items', 'filter'])
}
}
filterOnGroup: 'filter'
delegate: Rectangle {
width: 180
height: 30
border.width: 1
Text {
anchors.centerIn: parent
text: index
}
}
}
}
}
}
并且不要使用 Loader
作为代表。委托是动态实例化的,因此 Loader
只是无用的开销。您可以在委托中使用 Loader
作为通常不显示的部分。
我是初学者QT/QML应用程序开发
如何根据 ListModel 计数动态创建 qml。 在视图中,我使用 Repeater 在 GridLayout 中列出了模型对象。
Item{
id:griditem
anchors.fill:parent
GridLayout{
id: grid2
x:145
y:30
Layout.preferredHeight: 480
Layout.preferredWidth: 1135
rowSpacing:10
columnSpacing:40
columns: 3
rows: 2
Repeater{
id: repeater_Id
model: FeatureModel{}
Loader{
id: loader_Id
source: "QuadTiles.qml"
onLoaded: {
loader_Id.item.nIndex=index
loader_Id.item.type_String = type
loader_Id.item.title_Text.text = title
loader_Id.item.description_Text.text = description
loader_Id.item.btn1_icon.source = icon1
}
}
} //Repeater
}//GridLayout
}
编辑: 我面临一些问题 我需要根据 ModelList 计数动态创建新视图。 GridLayout
中每页最多有 6 个项目(3 行和 2 列)'QuadTiles.qml'是加载到GridLayout的每一项的qml文件
尝试这样的事情:
lm
是要拆分的 ListModel
。
SwipeView {
width: 200
height: 800
clip: true
currentIndex: 0
Repeater {
model: Math.ceil(lm.count / 6)
delegate: ListView {
width: 200
height: 800
property int viewIndex: index
model: DelegateModel {
model: lm
groups: DelegateModelGroup { name: 'filter' }
Component.onCompleted: {
for (var i = viewIndex * 6; i < lm.count && i < (viewIndex * 6) + 6; i++) {
items.setGroups(i, 1, ['items', 'filter'])
}
}
filterOnGroup: 'filter'
delegate: Rectangle {
width: 180
height: 30
border.width: 1
Text {
anchors.centerIn: parent
text: index
}
}
}
}
}
}
并且不要使用 Loader
作为代表。委托是动态实例化的,因此 Loader
只是无用的开销。您可以在委托中使用 Loader
作为通常不显示的部分。