简体   繁体   中英

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.

By default, currentIndex is set to 0 , which is why the first item always starts highlighted. You can turn that off by simply initializing currentIndex to -1.

ListView {
    currentIndex: -1
    ...
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM