qt - 为什么我的亮点项目快速显示和消失?
问题描述
我写了一个简单的ListView
并且想在按下键盘箭头键时突出显示该项目。
程序看起来不错,但是高亮功能工作起来很奇怪,它快速显示并且在我眼中消失。这不是我想要的效果,我希望它在当前项目高亮时始终显示高亮颜色。
import QtQuick 2.15
import QtQuick.Window 2.15
Rectangle {
width: 400
height: 400
Component {
id: nameDelegate
Rectangle {
width: ListView.view.width
height: 30
Text {
anchors.centerIn: parent
text: index
font.pixelSize: 14
}
}
}
ListView {
anchors.fill: parent
spacing: 5
focus: true
model: 10
delegate: nameDelegate
highlight: Rectangle {
width: ListView.view.width
height: ListView.view.currentItem.height
color: "lightGray"
}
}
}
添加新信息
如果我将委托项目(矩形)更改为下面
Rectangle {
width: ListView.view.width
height: 30
color: "#ada"
Text {
anchors.centerIn: parent
text: index
font.pixelSize: 14
}
}
而且我想在按下箭头键时突出显示项目显示浅灰色,我该怎么做?
解决方案
这是因为堆叠顺序。
高亮的默认z
值为0
. 每个委托的z
值为1
,因此默认情况下,突出显示在每个委托的后面。
您的代表有一个白色Rectangle
作为其背景,因此突出显示将不可见。
您可以从代理中删除白色背景(例如,只使用普通的Item
或将color
矩形设置为透明)或增加高亮的 z 值。但是,后者将导致文本不可见,因此您必须重新考虑突出显示的外观。例如,您可以将其设置为下划线而不是纯色块:
ListView {
anchors.fill: parent
spacing: 5
focus: true
model: 10
delegate: nameDelegate
highlight: Item {
z: 2
Rectangle {
width: parent.width
height: 1
y: parent.height - height
color: "lightGray"
}
}
}
推荐阅读
- javascript - 从电子应用程序表单的输入中复制选定的文件
- node.js - Recaptcha 使用 catch 处理 Promise 中的抛出异常
- html - 包含像 {{ $title }} 这样的表达式的示例代码导致 Laravel 中出现空白页
- python - Cartopy:无法绘制具有不确定性的矢量场(及相关问题)
- ios - 在静态库中向 Swift 公开 Objc 私有标头
- reactjs - 扩展接口或创建类似类型的功能
- reactjs - 反应本机地图方向:无法读取未定义的属性“地图”
- ios - iOS SwiftUI:防止 resizable() 动画
- html - 使用 CSS 创建自定义复选框
- javascript - 除非已经在 Javascript 中编码,否则对 XML 实体进行编码