首页 > 解决方案 > 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;

           }
        }
    }

标签: qtqmlqt5

解决方案


推荐阅读