qt - QML 树视图工具提示
问题描述
我试图仅当鼠标悬停在项目上时才在树视图中显示每个项目的工具提示,因此自定义了 itemDelegate。问题是,当我单击带有子项的父项时,它在展开父项时显示的工具提示与子项的数量一样多。这是意料之中的。当然,如果我单击 child(leaf) 项目,它工作得很好。展开项目时,似乎每个子项目都会发生悬停事件。防止这种情况的解决方案是什么?这是我的代码片段。
TreeView {
id: tableViewMeasurementList
anchors.fill: parent
anchors.margins: 5
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
verticalScrollBarPolicy: Qt.ScrollBarAsNeeded
model: measurementList.measurementList
selection: ItemSelectionModel {
id: measurementSelectionModel
model: measurementList.measurementList
}
TableViewColumn {
title: qsTr("Name")
role: "name"
}
TableViewColumn {
title: qsTr("Value")
role: "value"
}
}
tableMeasurementList.itemDelegate: Rectangle {
color: "transparent"
Text {
id: rowText
anchors.verticalCenter: parent.verticalCenter
text: styleData.value
font.family: "Verdana"
font.pixelSize: 16
}
ToolTip {
id:tableItemTooltip
visible: tableItem.pressed
font.pixelSize: 16
delay: 100
timeout: 2500
background: Rectangle {
border.color: "red"
color: "white"
opacity: 0.9
implicitWidth: 200
}
contentItem: Text {
text: tableItemTooltip.text
font: tableItemTooltip.font
color: "black"
}
}
MouseArea {
id:tableItem
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
hoverEnabled: true
onClicked: {
if( styleData.depth === 0 && styleData.column === 0 ) {
// collapse the last node
// expand the current node
}
}
onEntered: {
var mName = "sample tooltip";
tableItemTooltip.text = mName;
tableItemTooltip.y = tableItem.y + tableItem.height;
}
}
}
解决方案
推荐阅读
- php - 如何为 php error_log 文件设置不同的路径?
- sql - 查询日期时间声明
- javascript - array.sort() 函数中的“无法读取未定义的属性‘formatTime’”错误
- scala - Date_format 转换为边界日期增加 1 年
- python-3.x - 使用python解析yaml
- xmpp - 如何在不发送自我存在的情况下接收消息节?
- c# - EXE中的C#调用方法
- java - setOnKeyListener 函数适用于某些手机,但不适用于其他手机
- jquery - 对 jQuery AJAX 请求进行分组以强制执行顺序
- r - Dplyr 相当于 SUM over PARTITION BY