内部人员首先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
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
想法欢迎。谢谢,
如果propagateComposedEvents设置为true,则组成的事件将自动传播到MouseAreas场景中相同位置的其他事件。每个事件都将MouseArea按照堆叠顺序传播到其下方的下一个启用的对象,在此视觉层次结构中向下传播,直到a MouseArea接受该事件。一旦事件在层次结构中向下传播,直到发生另一个鼠标事件之前,就无法使其上升到层次结构中。因此,当您mouse.accepted = false在蓝色矩形中进行设置时,鼠标事件将转到黄色矩形,并且会同时接收到pressed和released发出信号,但是上方的矩形将不再接收任何事件,直到发生另一个鼠标事件为止。因此,答案是否定的。
如果您想在不同级别上处理鼠标事件,例如,如果您要一个MouseArea处理clicked信号,另一个要处理信号pressAndHold,或者如果您希望一个MouseArea时间处理clicked大部分时间,但是在满足某些条件时将其传递,则以下示例将对您有所帮助
import QtQuick 2.0
Rectangle {
color: "yellow"
width: 100; height: 100
MouseArea {
anchors.fill: parent
onClicked: console.log("clicked yellow")
}
Rectangle {
color: "blue"
width: 50; height: 50
MouseArea {
anchors.fill: parent
propagateComposedEvents: true
onClicked: {
console.log("clicked blue")
mouse.accepted = false
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5654 次 |
| 最近记录: |