【问题标题】:QML ToolTip for Component组件的 QML 工具提示
【发布时间】:2023-03-12 05:12:01
【问题描述】:

我想在TableView 中为代表Component 创建一个ToolTip。它工作正常,如果我使用ItemDelegate,但是,我不能使用ItemDelegate,因为它破坏了TableView 的选择模式。如果我尝试在我的委托组件中定义ToolTip,我会收到错误

 qrc:/FileSystem.qml:34 Invalid component body specification

如何为Component 使用工具提示?

这是我的代码:

TreeView {
    id: view
    anchors.fill: parent
    sortIndicatorVisible: true
    model: fileSystemModel
    rootIndex: rootPathIndex
    selection: sel
    selectionMode: 2
    Component {
        id: mycomp
        Row{
            id: myrow
            CheckBox{
                id: cbox
                anchors.baseline: ctext.baseline
            }
            Text{
                id: ctext
                text: styleData.value
                color: styleData.textColor
                width: namecolumn.width-cbox.width-myrow.x
                elide: Text.ElideRight
            }
        }
        NC.ToolTip {
            parent: mycomp
            visible: hovered
            delay: 1000
            text: qsTr(styleData.value)
        }
    }

    TableViewColumn {
        id: namecolumn
        title: "Name"
        role: "fileName"

        resizable: true
        width: parent.width-sizeWidth-dateWidth-scrollBarWidth
        delegate: mycomp

    }

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    一个组件必须只包含一个“子”。因此,您可以尝试将组件的内容包装到一个 Item 中,如下所示:

    Component {
        Item {
            width: parent.width
            height: myrow.height
    
            Row{
                id: myrow
                CheckBox{
                    id: cbox
                    anchors.baseline: ctext.baseline
                }
                Text{
                    id: ctext
                    text: styleData.value
                    color: styleData.textColor
                    width: namecolumn.width-cbox.width-myrow.x
                    elide: Text.ElideRight
                }
            }
            MouseArea {
                id: mouseArea
                anchors.fill: true
                hoverEnabled: true
            }
            NC.ToolTip {
                visible: mouseArea.containsMouse
                delay: 1000
                text: qsTr(styleData.value)
            }
        }
    }
    

    【讨论】:

    • 谢谢。还有一点:使用这个解决方案,鼠标区域会阻塞 TableView 的选择模式。
    • 我会提出一个关于这个的新问题
    • 对不起,我的错。查看 MouseArea 的接受按钮属性:doc.qt.io/qt-5/qml-qtquick-mousearea.html#acceptedButtons-prop。只需将其设置为 Qt.NoButton。这应该可以解决问题。
    • 谢谢,也许在这里发布答案,因为我已经提出了一个新问题(可能有点太快了)stackoverflow.com/questions/50103461/… 是的,Qt.NoButton 可以解决问题 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2022-01-24
    • 2011-01-07
    相关资源
    最近更新 更多