QML ListVIew Item Pos



ListView有20个项目,鼠标点击得到第10个项目X位。

但是positionViewAtIndex函数也可以移动项目

有没有办法得到项目X pos没有positionViewAtIndex?

这是样本。

Window {
id: frame
width: 640
height: 480
visible: true
MouseArea{
anchors.fill: parent
onClicked: {
listView.positionViewAtIndex(10, ListView.SnapPosition);
var destPos = listView.contentX;
console.log("item10 x: " + destPos)
}
}
Rectangle {
width: 480
height: 80
color: "white"
ListView {
id:listView
anchors.fill: parent
anchors.margins: 20
clip: true
model: 20
orientation: ListView.Horizontal
delegate:
Rectangle {
width: 40
height: 40
color: "green"
Text {
anchors.centerIn: parent
text: index
}
}
spacing: 5
}
}
}

使用获取位置的方法,但如果想要居中位置,则需要更多的计算。

MouseArea{
anchors.fill: parent
onClicked: {
var destPos = listView.itemAtIndex(10).x
console.log("item10 x: " + destPos)
listView.contentX = destPos
}
}

最新更新