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

为何主组件禁用时MouseArea仍响应hover事件?

问题原因与解决方案

你的问题根源在于MouseArea的containsMouse属性不受enabled状态影响:即使MouseArea因继承父元素的enabled: false而被禁用,只要hoverEnabled设为true,该属性仍会根据鼠标位置实时更新,这就导致你看到的“禁用状态下鼠标悬停仍变红”的现象。

解决方案一:颜色判断时加入启用状态检查

直接在颜色计算逻辑中过滤组件的禁用状态,确保只有活跃时才响应鼠标悬停:

Rectangle {
  width: 100
  height: 100
  enabled: false
  color: (enabled && mouseArea.containsMouse) ? "red" : "green"

  MouseArea {
    id: mouseArea
    anchors.fill: parent
    hoverEnabled: true
  }
}

解决方案二:彻底禁用MouseArea的鼠标检测

将MouseArea的enabled和hoverEnabled与父元素的状态绑定,让它在父元素禁用时完全停止所有鼠标相关逻辑:

Rectangle {
  width: 100
  height: 100
  enabled: false
  color: mouseArea.containsMouse ? "red" : "green"

  MouseArea {
    id: mouseArea
    anchors.fill: parent
    hoverEnabled: parent.enabled
    enabled: parent.enabled
  }
}

两种方案都能实现你预期的“禁用状态下矩形保持绿色”的效果,可根据实际需求选择。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.01 17:33:10