[英]highlight QML ListView Item 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
}
我已经完成了鼠标部分,但我坚持在项目附加时禁用突出显示。
默认情况下, currentIndex
设置为0
,这就是为什么第一个项目总是开始突出显示的原因。 您可以通过简单地将currentIndex
初始化为 -1 来关闭它。
ListView {
currentIndex: -1
...
}
好的,经过几个小时的搜索,我使用了onCountChanged
信号来指示添加到列表中的项目,然后 rest 将ListView
currentIndex
设置为-1
,所以代码将是这样的
onCountChanged: {
list.currentIndex = -1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.