【发布时间】:2017-02-28 10:07:19
【问题描述】:
内部MouseArea 首先获取鼠标事件。我想“看到”这些事件,以便设置各种属性,但不影响它们。我希望鼠标事件传播到任何父 MouseArea。
考虑一下这段代码。我想点击蓝色方块来查看“按下蓝色”和“释放蓝色”以及传递给“按下父级”和“释放父级”。
如果我接受该事件,则父母不会得到它。如果我不接受按下,那么我看不到释放。
import QtQuick 2.7
import QtQuick.Controls 1.4
ApplicationWindow
{
visible: true
width: 800
height: 1024
Rectangle
{
anchors.fill: parent
color: "yellow"
MouseArea
{
// i want these to happen even when mouse events are in the
// blue square
anchors.fill: parent
onPressed: console.log("parent pressed");
onReleased: console.log("parent released");
}
Rectangle
{
x: 100
y: 100
width: 100
height: 100
color: "blue"
// i would like to "see" events, but not affect them
// i want all mouse events to pass to parent, as if i am not here.
// however, not accepting "pressed" means i don't see "released"
MouseArea
{
anchors.fill: parent
onPressed:
{
console.log("blue pressed");
mouse.accepted = false
}
onReleased:
{
console.log("blue released");
mouse.accepted = false
}
}
}
}
}
欢迎提出想法。谢谢,
【问题讨论】:
-
根据文档,设置
mouse.accepted无效。您可以改为使用onCancelled,如here中所述 -
onCancelled在另一个MouseArea声明信号时无助于跟踪onReleased。另一个答案可能是正确的,但可能更清楚:“使用MouseArea是不可能的”。其余的在他看来纯粹是基于意见,甚至没有尝试解决问题。相反,他的解决方案包括预先确定所有位置的静态 UI,或者您需要跟踪位置。至少 MouseArea1 需要了解 MouseArea2 的用途和状态。我认为这根本不是可取的。 -
在这个例子中,
onCanceled没有被调用,这就是accepted是否被清除。 Qt5.8.0