gpt4 book ai didi

qt qml。 MouseArea 可以看到事件,但将它们全部传递给父级而不影响它们吗?

转载 作者:行者123 更新时间:2023-12-02 01:16:04 30 4
gpt4 key购买 nike

内部MouseArea首先获取鼠标事件。我想“看到”这些事件,以便设置各种属性,但不影响它们。我希望鼠标事件传播到任何父 MouseArea

考虑这段代码。我想单击蓝色方 block 来查看“蓝色按下”和“蓝色释放”以及传递给“父级按下”和“父级释放”。

如果我接受该事件,家长不会收到它。如果我不接受按下,那么我看不到释放。

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 时,鼠标事件将转到黄色矩形,并接收 pressedreleased > 信号,但上部矩形将不再接收任何事件,直到另一个鼠标事件发生。所以,答案是否定的。

如果您想要处理不同级别的鼠标事件,例如您希望一个 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
}
}
}
}

关于qt qml。 MouseArea 可以看到事件,但将它们全部传递给父级而不影响它们吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505727/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com