QML(24)——提示文本ToolTip的使用

效果展示

在这里插入图片描述

原理说明

实现提示文本功能,需要设置两个属性。 关键是第二个

ToolTip.text:    // import QtQuick.Controls 2.15
ToolTip.visible:

如果组件自身含有 hovered 属性(Button),则可以

ToolTip.visible: hovered   //悬停时

如果组件自身不含 hovered 属性(Text, Rectangle),则需要利用MouseArea

ToolTip.visible: mousearea.containsMouse
MouseArea {
    id: mousearea1
    anchors.fill: parent
    hoverEnabled: true
}

代码

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

ColumnLayout {
    id: root
    property int textSize: 20
    property int componentSize:  30
    spacing: 30

    Button{
        Layout.alignment: Qt.AlignHCenter
        text: "Button"
        font.pixelSize: textSize
        ToolTip.visible: hovered
        ToolTip.text: "This is Button"
    }

    RoundButton {
        Layout.alignment: Qt.AlignHCenter
        icon.width: componentSize
        icon.height: width
        icon.source: "qrc:/resource/volume.png"
        icon.color: "Green"
        ToolTip.text: "This is RoundButton"
        ToolTip.visible: hovered
    }

    Text {
        Layout.alignment: Qt.AlignHCenter
        text: qsTr("Text")
        font.pixelSize: textSize
        ToolTip.text: "This is Text"
        ToolTip.visible: mousearea1.containsMouse
        MouseArea {
            id: mousearea1
            anchors.fill: parent
            hoverEnabled: true
        }
    }

    Label {
        Layout.alignment: Qt.AlignHCenter
        text: "Label"
        font.pixelSize: textSize
        ToolTip.text: "This is Label"
        ToolTip.visible: mousearea2.containsMouse
        MouseArea {
            id: mousearea2
            anchors.fill: parent
            hoverEnabled: true
        }
    }

    Rectangle {
        Layout.alignment: Qt.AlignHCenter
        Layout.fillWidth: true
        Layout.preferredHeight: 40
        color: "green"
        Text {
            anchors.centerIn: parent
            text: qsTr("Rectangle")
            font.pixelSize: textSize
        }
        ToolTip.text:"This is Rectangle"
        ToolTip.visible: mousearea3.containsMouse
        MouseArea {
            id: mousearea3
            anchors.fill: parent
            hoverEnabled: true
        }
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值