Search code examples
qtlistviewqmlqqmlcomponent

highlight QML ListView Item on mouse click


I have noticed that the listview will highlight the first item automatically/by defualt how can I disable it and only highlight the item item I have selected on mouse click?

Component {
id: highlight
Rectangle {
    width: 180; height: 40
    color: "lightsteelblue"; radius: 5
    y: list.currentItem.y
    Behavior on y {
        SpringAnimation {
            spring: 3
            damping: 0.2
        }
    }
  }
}

ListView {
   id: list
   width: 180; height: 200
   model: ContactModel {}
   delegate: Text {
    text: name 
        
      MouseArea{
          anchors.fill: parent
          onClicked: {
          list.currentIndex = index
         }
      }
    }

   highlight: highlight
   highlightFollowsCurrentItem: false
   focus: true
}

I have aleardy done the mouse part but i'm stuck in disabling the highlight at item appending.


Solution

  • Okay after several hours of searching I have used the onCountChanged signal to indicate the items addition into the list and then rest the ListView currentIndex to -1 so the code will be like this

    onCountChanged: {
       list.currentIndex = -1
    }