简体   繁体   English

鼠标单击时突出显示 QML ListView 项目

[英]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.默认情况下, currentIndex设置为0 ,这就是为什么第一个项目总是开始突出显示的原因。 You can turn that off by simply initializing currentIndex to -1.您可以通过简单地将currentIndex初始化为 -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信号来指示添加到列表中的项目,然后 rest 将ListView currentIndex设置为-1 ,所以代码将是这样的

onCountChanged: {
   list.currentIndex = -1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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