Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ListView: disable auto selection



  • Hello,
    i'm facing a small problem with ListView which is the item auto selection at the start.

    i created a search bar and when i search for something, it will append items to the listview, it works well but it auto select the first item of the list and i dont want that.
    i want that it select only when i click on a specific item

    so is there a way to do that?

    here is a part of my code:

    ListModel {
        id: lm    
    }
    Rectangle {
        width: drawer.width; height: drawer.height
        Component {
            id: contactDelegate
            Item {
                width: drawer.width; height: 100
                Row {
                    Image { width: 80; height: 100; source: posterSource}
                    Text { text: '<b>Title:</b> ' + title }
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: lv.currentIndex = index
                }
            }
        }
        ListView {
            id: lv
            anchors.fill: parent
            model: lm
            delegate: contactDelegate
            highlight: Rectangle { color: "#2196f3"; }
            focus: true
            onCurrentItemChanged: console.log(lm.get(lv.currentIndex).title + ' selected')
        }
    }
    

    Thank you



  • @mIcHyAmRaNe Have you tried to set currentIndex to -1?

        ListView {
            id: lv
            anchors.fill: parent
            currentIndex:-1
            model: lm
            delegate: contactDelegate
            highlight: Rectangle { color: "#2196f3"; }
            focus: true
            onCurrentItemChanged: console.log(lm.get(lv.currentIndex).title + ' selected')
        }
    


  • This post is deleted!


  • @KroMignon Thank you, it works


Log in to reply