0

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MDkman
  • 67
  • 2
  • 13

2 Answers2

0

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
    ...
}
JarMan
  • 7,589
  • 1
  • 10
  • 25
  • Thank you for the quick response, unfortunaltly i have tried your solution, but it didn't work with me. it still highlight the first added item. BTW I'm using Qt version 5.9 – MDkman Dec 27 '20 at 05:26
  • I tested it with the code you provided and it did work. Maybe your application is doing something that your example code is not? – JarMan Dec 27 '20 at 18:43
  • well in my application i add the items into the list dynamically, but in the example since it use it in the initialization it's working fine. so is there another way to disable it while items are added into the listview? @JarMan – MDkman Dec 28 '20 at 11:06
  • Please edit your question to provide a better example that demonstrates the problem. – JarMan Dec 28 '20 at 15:53
0

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
}
MDkman
  • 67
  • 2
  • 13