如何阻止并且不将 MouseArea 的 containsMouse 属性 传播给父级?
How to block and do not propagate containsMouse property of MouseArea to parent?
您应该保持父子关系,以便将 MouseArea
的移动事件传播到其他重叠且视觉堆叠顺序较低的 MouseArea
。 *
但是如何做相反的事情,那就是如果我不想共享移动事件,如何阻止移动信号,所以父 MouseArea 不会 containsMouse
而子 containsMouse
(假设两者都有 hoverEnabled: true
)?
编辑:
这里有一个小示例应用程序来说明我在说什么。
基本上我正在寻找一些优雅的(qml 仅在可能的情况下,但任何解决方案都会受到赞赏)外部 MouseArea
(ma1
) 使 containsMouse
等于 false
当鼠标悬停在内部 MouseArea 上时。
import QtQuick 2.12
import QtQuick.Controls 2.5
ApplicationWindow{
width: 640
height: 480
visible: true
MouseArea {
id: ma1
anchors.fill: parent
hoverEnabled: true
MouseArea {
anchors.centerIn: parent
width: parent.width * Math.log(2)
height: parent.height * Math.log(2)
hoverEnabled: true
z: 1
// onPositionChanged: mouse.accepted = true;
// onMouseXChanged: mouse.accepted = true;
// onMouseYChanged: mouse.accepted = true;
// onEntered: { ma1.hoverEnabled = false; ma1.enabled = false; }
// onExited: ma1.hoverEnabled = true;
Component.onCompleted: bg.createObject(this, { "hovered": Qt.binding(function() { return containsMouse; }) } );
}
Component.onCompleted: bg.createObject(this, { "hovered": Qt.binding(function() { return containsMouse; }) } );
}
Component {
id: bg
Rectangle {
property bool hovered: false
anchors.fill: parent
color: hovered ? "green" : "red"
border.width: 1
Text {
anchors {
top: parent.top
horizontalCenter: parent.horizontalCenter
}
text: (!parent.hovered ? "un" : "") + "hovered"
color: "white"
}
}
}
}
实际上,请求的功能应该开箱即用。每次卡在某个地方时,尝试从头开始创建单独的项目。
看看我为你准备的bit simpler code。它是纯 QML。此外,由于所有内容均按逻辑顺序放置 - 无需提供堆叠顺序(z
参数)。
因此,根据 Qt 支持,无法在保持两者之间的 parent-child 关系的同时使用 containsMouse
属性 执行此操作MouseArea
s.
...the workaround for this situation can be breaking the parent-child
relationship or having another [custom] property...
您应该保持父子关系,以便将 MouseArea
的移动事件传播到其他重叠且视觉堆叠顺序较低的 MouseArea
。 *
但是如何做相反的事情,那就是如果我不想共享移动事件,如何阻止移动信号,所以父 MouseArea 不会 containsMouse
而子 containsMouse
(假设两者都有 hoverEnabled: true
)?
编辑:
这里有一个小示例应用程序来说明我在说什么。
基本上我正在寻找一些优雅的(qml 仅在可能的情况下,但任何解决方案都会受到赞赏)外部 MouseArea
(ma1
) 使 containsMouse
等于 false
当鼠标悬停在内部 MouseArea 上时。
import QtQuick 2.12
import QtQuick.Controls 2.5
ApplicationWindow{
width: 640
height: 480
visible: true
MouseArea {
id: ma1
anchors.fill: parent
hoverEnabled: true
MouseArea {
anchors.centerIn: parent
width: parent.width * Math.log(2)
height: parent.height * Math.log(2)
hoverEnabled: true
z: 1
// onPositionChanged: mouse.accepted = true;
// onMouseXChanged: mouse.accepted = true;
// onMouseYChanged: mouse.accepted = true;
// onEntered: { ma1.hoverEnabled = false; ma1.enabled = false; }
// onExited: ma1.hoverEnabled = true;
Component.onCompleted: bg.createObject(this, { "hovered": Qt.binding(function() { return containsMouse; }) } );
}
Component.onCompleted: bg.createObject(this, { "hovered": Qt.binding(function() { return containsMouse; }) } );
}
Component {
id: bg
Rectangle {
property bool hovered: false
anchors.fill: parent
color: hovered ? "green" : "red"
border.width: 1
Text {
anchors {
top: parent.top
horizontalCenter: parent.horizontalCenter
}
text: (!parent.hovered ? "un" : "") + "hovered"
color: "white"
}
}
}
}
实际上,请求的功能应该开箱即用。每次卡在某个地方时,尝试从头开始创建单独的项目。
看看我为你准备的bit simpler code。它是纯 QML。此外,由于所有内容均按逻辑顺序放置 - 无需提供堆叠顺序(z
参数)。
因此,根据 Qt 支持,无法在保持两者之间的 parent-child 关系的同时使用 containsMouse
属性 执行此操作MouseArea
s.
...the workaround for this situation can be breaking the parent-child relationship or having another [custom] property...