【问题标题】:qt qml. Can a MouseArea see events, but pass them all to parent without affecting them?qt qml。 MouseArea 可以看到事件,但将它们全部传递给父级而不影响它们吗?
【发布时间】: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

标签: qt qml qtquick2


【解决方案1】:

如果 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
            }
        }
    }
}

【讨论】:

  • 同意。答案是“不,做不到”。记住这一点,最好有propagatgeAllEvents 或类似的东西。 +1 有关其他传播事件的建议,因此接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 2012-10-31
  • 1970-01-01
相关资源
最近更新 更多