QML ToolTip 组件:图标、多行文本与阴影

ToolTip 的 contentItem 可以塞入完整布局。这篇文章做一个带图标、多行文本和阴影的富文本提示。

富文本 Tooltip

悬停在粉色矩形上,显示带图标、多行文本和阴影的 Tooltip。

演示代码

qml 复制代码
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Qt5Compat.GraphicalEffects

FadeInAnimation {
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 15

        TitleSeparator {
            title: "富文本 Tooltip"
            description: "在 contentItem 中使用布局、图标与多行文本,并为背景添加阴影效果"
        }

        RowLayout {
            Layout.preferredHeight: 120
            Layout.alignment: Qt.AlignHCenter
            spacing: 20

            Rectangle {
                Layout.preferredWidth: 120
                Layout.preferredHeight: 40
                color: "lightpink"
                radius: 4

                Text {
                    anchors.centerIn: parent
                    text: "富文本 Tooltip"
                }

                ToolTip {
                    id: richTooltip
                    delay: 500
                    timeout: 5000
                    visible: richMA.hovered

                    contentItem: ColumnLayout {
                        Text {
                            text: "富文本提示"
                            font.bold: true
                            color: "#E91E63"
                        }
                        Image {
                            Layout.preferredWidth: 32
                            Layout.preferredHeight: 32
                            source: "qrc:/qt/qml/qml_tooltip/images/info.svg"
                        }
                        Text {
                            text: "包含图标和多行文本的tooltip"
                            color: "#333333"
                        }
                    }

                    background: Rectangle {
                        color: "#FFFFFF"
                        radius: 8
                        border.color: "#E0E0E0"
                        border.width: 1

                        layer.enabled: true
                        layer.effect: DropShadow {
                            transparentBorder: true
                            horizontalOffset: 2
                            verticalOffset: 2
                            radius: 8.0
                            samples: 17
                            color: "#80000000"
                        }
                    }
                }

                HoverHandler {
                    id: richMA
                }
            }
        }

        Item { Layout.fillHeight: true }
    }
}

关键逻辑解析

  • contentItemColumnLayout 组合标题、图标和说明文字。
  • Imagesource 使用 qrc:/ 资源路径,图片需提前加入资源文件。
  • 背景通过 layer.enabled + DropShadow 实现阴影,需要导入 Qt5Compat.GraphicalEffects
  • transparentBorder: true 避免阴影被裁切。

适用场景:复杂信息提示、带图标的操作说明、卡片式浮层。

运行验证

  1. Qt Creator 打开 qml_tooltip/CMakeLists.txt
  2. Ctrl+R 运行;
  3. 悬停在粉色矩形上,观察富文本、图标和阴影效果。

扩展复用方向

  • contentItem 里加 SliderButton,做成可交互的浮层。
  • 封装成 InfoTooltip 组件,传入标题、图标、说明文字即可复用。

已验证环境

相关推荐
我不是疯子是傻子13 小时前
串口通信数据帧粘包拆包再进化:基于 Qt 的滑动窗口与 CRC 校验实战
开发语言·qt
程序员老陆13 小时前
Qt中实现窗口真全屏(覆盖Windows任务栏)
开发语言·windows·qt
艾莉丝努力练剑17 小时前
【QT:解决问题】Qt5Core.dll:无法定位程序输入点
java·开发语言·qt·学习·面试
秋田君2 天前
QT_JSON文件操作
开发语言·qt·json
大白同学4212 天前
初识Qt——信号和槽
开发语言·qt
秋田君2 天前
QT_INI文件操作
开发语言·qt
≮傷£≯√2 天前
FFmpeg + qt 音频播放
qt·ffmpeg·音视频
youqingyike2 天前
【无标题】
前端·qt
程序员老陆2 天前
Qt::WidgetAttribute 常用属性详解
开发语言·qt
程序员老陆2 天前
Qt的QThread::usleep和FFmpeg的libavutil模块的av_usleep哪个精度高一些?
开发语言·qt·ffmpeg·音视频