You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QtQuick:嵌套Item内的ToolTip鼠标释放后提前消失如何解决?

问题解决方法

核心原因是ToolTip默认关闭策略包含ToolTip.CloseOnRelease,鼠标释放时会直接关闭提示,无视设置的超时时间。嵌套在Item中时该逻辑依然生效,导致提示提前消失。

解决步骤

给嵌套的ToolTip修改closePolicy属性,移除CloseOnRelease规则,仅保留超时关闭的逻辑,同时保留Item的锚点布局实现底部定位需求。

修改后的代码示例

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

Window {
    width: 640
    height: 480
    visible: true

    ColumnLayout {
        anchors.fill: parent

        DelayButton {
            Layout.fillWidth: true
            text: "Save"
            onActivated: {
                checked = false
                progress = 0
                tt.show("Saved", 5000)
            }

            onPressed: ta.text = "Pressed"
            onReleased: ta.text = "Released"
        }

        TextArea {
            id: ta
            Layout.fillHeight: true
            Layout.fillWidth: true
            text: "Foo bar"
        }
    }

    Item {
        id: tt
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter

        function show(text, ms) {
            if (ms === undefined)
                toolTip.show(text)
            else
                toolTip.show(text, ms)
        }

        ToolTip {
            id: toolTip
            timeout: 3000
            font.bold: true
            // 仅在超时后自动关闭提示
            closePolicy: ToolTip.CloseOnTimeout
        }
    }
}

额外说明

closePolicy支持以下可选值,可根据业务需求组合:

  • ToolTip.CloseOnPress:鼠标按下时关闭提示
  • ToolTip.CloseOnRelease:鼠标释放时关闭提示(默认包含此规则)
  • ToolTip.CloseOnTimeout:超时后关闭提示
  • ToolTip.NoAutoClose:不自动关闭,需手动调用hide()方法

内容的提问来源于stack exchange,提问作者Me3nTaL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 15:04:55