ToolTip QML Type

为任何控件提供工具提示。更多

Import Statement: import QtQuick.Controls
Inherits:

Popup

属性

附属物业

方法

  • void hide() (since QtQuick.Controls 2.5 (Qt 5.12))
  • void show(string text, int timeout) (since QtQuick.Controls 2.5 (Qt 5.12))

附加方法

  • void hide()
  • void show(string text, int timeout)

详细说明

工具提示是一段简短的文字,用于告知用户控件的功能。它通常位于父控件的上方或下方。提示文本可以是任何富文本格式的字符串。

附加工具提示

为控件设置工具提示的最直接方法是通过附加属性指定textvisibility 。下面的示例说明了这种方法:

Button {
    text: qsTr("Save")

    ToolTip.visible: down
    ToolTip.text: qsTr("Save the active project")
}

在正常情况下,一次只能看到一个工具提示。为了节省资源,所有使用工具提示附加属性的项目都共享同一个可视化工具提示标签实例。尽管视觉效果是共享的,但texttimeoutdelay 会为使用相应附加属性的每个项目单独存储。但是,多个项目不能同时显示共享工具提示。共享工具提示只显示给最后一个使其可见的项目。共享工具提示的位置由框架决定。

注意: 要自定义attached ToolTip ,必须将其作为自己风格的一部分。要对ToolTip 进行一次性定制,请参阅Custom Tool Tips

延迟和超时

工具提示通常是瞬时的,即在某个外部事件或用户交互后显示,并且通常在某个超时后隐藏。可以控制工具提示显示时的延迟和隐藏时的超时。这样就可以实施不同的工具提示显示和隐藏策略。

例如,在触摸屏上,显示工具提示的常见模式是按住按钮不放。下面的示例演示了如何延迟显示工具提示,直到达到按住的时间间隔。在此示例中,一旦松开按钮,工具提示就会隐藏。

Button {
    text: qsTr("Button")

    ToolTip.visible: pressed
    ToolTip.delay: Application.styleHints.mousePressAndHoldInterval
    ToolTip.text: qsTr("This tool tip is shown after pressing and holding the button down.")
}

但是,对于指针设备,可能需要在按钮悬停一段时间后才显示工具提示。下面的示例介绍了如何在按钮悬停一秒后显示工具提示,并在五秒超时后隐藏工具提示。

Button {
    text: qsTr("Button")
    hoverEnabled: true

    ToolTip.delay: 1000
    ToolTip.timeout: 5000
    ToolTip.visible: hovered
    ToolTip.text: qsTr("This tool tip is shown after hovering the button for a second.")
}

自定义工具提示

如果需要对工具提示位置进行更精细的控制,或者需要同时显示多个工具提示实例,也可以创建本地工具提示实例。这样就可以自定义工具提示,并使用整个Popup API。下面的示例展示了一个工具提示,在拖动手柄时显示滑块的值。

Slider {
    id: slider
    value: 0.5

    ToolTip {
        parent: slider.handle
        visible: slider.pressed
        text: slider.value.toFixed(1)
    }
}

另请参阅 自定义工具提示弹出控件closePolicy

属性文档

delay : int

该属性表示工具提示显示的延迟时间(毫秒)。延迟为负的工具提示会立即显示。默认值为0

另请参阅 Delay and Timeout


text : string

该属性用于保存工具提示上显示的文本。


timeout : int

该属性保存工具提示隐藏的超时(毫秒)。超时为负数的工具提示不会自动隐藏。默认值为-1

另请参阅 Delay and Timeout


附加属性文档

ToolTip.delay : int

此附加属性用于保存共享工具提示的延迟时间(毫秒)。该属性可附加到任何项目。

另请参阅 Attached Tool TipsDelay and Timeout


ToolTip.text : string

此附加属性保存共享工具提示的文本。该属性可附加到任何项目。

另请参阅 Attached Tool Tips


ToolTip.timeout : int

此附加属性保存共享工具提示的超时(毫秒)。该属性可附加到任何项目。

另请参阅 Attached Tool TipsDelay and Timeout


ToolTip.toolTip : ToolTip

此附加属性保存共享工具提示实例。该属性可附加到任何项目。

另请参阅 Attached Tool Tips


ToolTip.visible : bool

此附加属性保存共享工具提示是否可见。该属性可附加到任何项目。

另请参阅 Attached Tool Tips


方法文档

[since QtQuick.Controls 2.5 (Qt 5.12)] void hide()

本方法隐藏工具提示。

该方法在 QtQuick.Controls 2.5 (Qt 5.12) 中引入。


[since QtQuick.Controls 2.5 (Qt 5.12)] void show(string text, int timeout)

该方法以工具提示的形式显示text ,超时时间为timeout (毫秒)。

该方法在 QtQuick.Controls 2.5 (Qt 5.12) 中引入。


附加方法文档

void hide()

此附加方法可隐藏共享工具提示。该方法可附加到任何项目。

另请参阅 Attached Tool Tips


void show(string text, int timeout = -1)

此附加方法通过texttimeout (毫秒)显示共享工具提示。该方法可附加到任何项目。

另请参阅 Attached Tool Tips


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.