我正在 QML 中实现一个带有更多按钮的列表视图。列表和按钮看起来像这样“abcde moreButton”。(其中 abcd 和 e 是字符串的元素)并且我的列表将包含 5 个以上的元素。单击更多按钮后,我需要从列表中获取接下来的 5 个元素。说“fghij”,依此类推,直到显示列表中的所有元素。我的问题是如何从列表中获取前 5 个、第 2 个 5 等元素?任何帮助表示赞赏。
问问题
633 次
1 回答
2
You can do something like this :
ListView
{
id: view
x: 50
width: 200; height: 500
property variant alphabetArray: ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' ]
function append( )
{
// @ToDoyourself : Check all the boundary conditions in this function
var loopTime = alphabetModel.count
for( var i = loopTime ; i <= loopTime+5 ; ++ i )
alphabetModel.append({ "name" : alphabetArray[i] })
}
model: ListModel
{
id: alphabetModel
ListElement { name: "a" }
ListElement { name: "b" }
ListElement { name: "c" }
ListElement { name: "d" }
ListElement { name: "e" }
}
footer:
Text
{
text : "More" ; font.pixelSize: 35 ; color : "grey"
MouseArea
{
anchors.fill: parent
onClicked:
{
append()
}
}
}
delegate:
Item
{
height: 100
Text
{
text: name
height: 20
font.pixelSize: 30
color: index === view.currentIndex ? "black" : "red"
MouseArea
{
anchors.fill: parent
onClicked: view.currentIndex = index
}
}
}
}
Note: In the function append()
I haven't done any boundary checks on the array. Do that as an exercise yourself.
于 2013-07-03T06:18:34.810 回答