qt qml。 MouseArea 可以看到事件,但将它们全部传递给 parent 而不影响它们吗?
qt qml. Can a MouseArea see events, but pass them all to parent without affecting them?
内部MouseArea
首先获取鼠标事件。我想"see"这些事件,从而设置各种属性,但不影响它们。我希望鼠标事件传播到任何 parent MouseArea
.
考虑这段代码。我想单击蓝色方块以查看 "blue pressed" 和 "blue released" 以及传递给 "parent pressed" 和 "parent released"。
如果我接受事件,parent 不会接受。如果我不接受按下,那么我看不到释放。
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
}
}
}
}
}
欢迎提出想法。谢谢,
如果 propagateComposedEvents
设置为 true,则组合事件将自动传播到场景中同一位置的其他 MouseAreas
。每个事件都按堆叠顺序传播到其下方的下一个启用的 MouseArea
,沿着此视觉层次结构向下传播,直到 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
}
}
}
}
内部MouseArea
首先获取鼠标事件。我想"see"这些事件,从而设置各种属性,但不影响它们。我希望鼠标事件传播到任何 parent MouseArea
.
考虑这段代码。我想单击蓝色方块以查看 "blue pressed" 和 "blue released" 以及传递给 "parent pressed" 和 "parent released"。
如果我接受事件,parent 不会接受。如果我不接受按下,那么我看不到释放。
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
}
}
}
}
}
欢迎提出想法。谢谢,
如果 propagateComposedEvents
设置为 true,则组合事件将自动传播到场景中同一位置的其他 MouseAreas
。每个事件都按堆叠顺序传播到其下方的下一个启用的 MouseArea
,沿着此视觉层次结构向下传播,直到 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
}
}
}
}