QML How can I get the current index of an item in a ListView?

As an example:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    width: 250
    height: 250
    visible: true

    Rectangle {
        anchors.fill: parent

        ListModel {
            id: colorsModel
            ListElement { colorCode: "red"   }
            ListElement { colorCode: "green" }
            ListElement { colorCode: "blue"  }
            ListElement { colorCode: "orange"}
        }

        ListView {
            anchors.fill: parent
            snapMode: ListView.SnapOneItem
            model: colorsModel

            delegate: Rectangle {
                width: 250
                height: 250
                color: colorCode
            }

            onMovementEnded: {
                console.log($CurrentElementIndex$)
            }
        }
    }
}

      

How can I get the current index of an element in $ CurrentElementIndex $ placeholder?

ListView.currentIndex is a write property as if I understood correctly.

Perhaps I can use ListView.indexAt (), but I don't understand how it works.

+3


source to share


2 answers


onMovementEnded()

called when the list ends with scrolling. This is independent of the item selection. You can add the currently selected debug marker and use the handler onCurrentIndexChanged

:



ListView {
    anchors.fill: parent
    snapMode: ListView.SnapOneItem
    model: colorsModel

    delegate: Rectangle {
        width: 250
        height: 250
        color: colorCode
    }

    highlight: Rectangle {
        anchors.fill: parent
        color: "red"
    }

    onCurrentIndexChanged: console.log(currentIndex)

    // ...

      

+2


source


You can use currentIndex

to get and set the currently selected item. But you said:

I want a new index to appear while scrolling

so you need to set highlightRangeMode: ListView.StrictlyEnforceRange

to your ListView. This will change the property currentIndex

whenever you scroll through the list.
Here's an example:



ListView {
    id: list
    anchors.fill: parent
    model: 10
    orientation: ListView.Vertical
    snapMode: ListView.SnapOneItem
    highlightRangeMode: ListView.StrictlyEnforceRange     //<--This is the key note
    header: Component { Item { width: 1; height: 30 } }
    footer: Component { Item { width: 1; height: 30 } }
    preferredHighlightBegin: height/2-10    //<-----using these two lines you can control position of the selected item
    preferredHighlightEnd: height/2-10      //<--|
    currentIndex: 3
    onCurrentIndexChanged: {
        console.log("current selected index is: "+currentIndex);    //<-- Test output
    }

    delegate: Component {
        Text {
            id: _root
            width: parent.width
            height: 20
            horizontalAlignment: Text.AlignHCenter
            color: "#888"
            font { family: "Segoe UI Light"; pixelSize: 18 }
            text: index
        }
    }
}

      

See the Qt documentation for details.

+1


source







All Articles