Home > other >  QML ListVIew Item Pos
QML ListVIew Item Pos

Time:06-01

ListView have 20 items, mouse click to get the tenth item X pos.

But positionViewAtIndex function also move the item.

Is have any way to get Item X pos without positionViewAtIndex?

here is sample.

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
        }
    }
}

CodePudding user response:

using the way to get the position, but if want center position, it's need more calculate.

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