12

An inner MouseArea gets the mouse events first. I would like to "see" these events, so as to set various properties, but not affect them. I would like the mouse events to propagate to any parent MouseArea.

consider this code. I would like clicking on the blue square to see "blue pressed" and "blue released" as well as passing to "parent pressed" and "parent released".

If i accept the event, the parent does not get it. if i don't accept pressed, then i do not see released.

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

ideas welcome. thanks,

jkj yuio
  • 2,543
  • 5
  • 32
  • 49
  • According to the documentations, setting `mouse.accepted` has no effect. You could instead, use `onCancelled` as described in [here](http://doc.qt.io/qt-5/qml-qtquick-mousearea.html#canceled-signal) – DuKes0mE Feb 28 '17 at 10:34
  • Also possible duplicate to: http://stackoverflow.com/questions/23911433/qml-cant-get-mouse-released-event-when-i-dont-accept-mouse-pressed-event – DuKes0mE Feb 28 '17 at 10:40
  • `onCancelled` does not help to track the `onReleased` when another `MouseArea` claims the signals. The other answer is probably right, but could be more clear: *"with a `MouseArea` it is not possible"*. The rest is *in his opinion purely opinion-based and does not even try to solve the problem*. Rather his solution includes either static UIs where all positions are predetermined, or you need to track the positions. At least MouseArea1 needs knowledge of the purpose and state of MouseArea2. I don't think this is desirable at all. – derM - not here for BOT dreams Feb 28 '17 at 10:49
  • Possible duplicate of [QML - Can't get mouse released event when I don't accept mouse pressed event](http://stackoverflow.com/questions/23911433/qml-cant-get-mouse-released-event-when-i-dont-accept-mouse-pressed-event) – m7913d Feb 28 '17 at 10:59
  • with this example, `onCanceled` is not being called, that's whether `accepted` is cleared or not. Qt5.8.0 – jkj yuio Feb 28 '17 at 12:37
  • So it looks like the answer is "NO". The other question has an answer that says, do it a different way. In my case, i actually have a `MouseArea` inside a `Flickable` so i can't just change the `Flickable`. I'd like to see press/release and click without breaking the flick. – jkj yuio Feb 28 '17 at 12:40

1 Answers1

11

If propagateComposedEvents is set to true, then composed events will be automatically propagated to other MouseAreas in the same location in the scene. Each event is propagated to the next enabled MouseArea beneath it in the stacking order, propagating down this visual hierarchy until a MouseArea accepts the event. Once the event has traveled down the hierarchy there no way for it to come up the hierarchy until another mouse event occur. So, when you are setting mouse.accepted = false in the blue rectangle the mouse event goes to the yellow rectangle and it receives both pressed and released signals but the upper rectangle will no longer receive any events untill another mouse event occurs. So, the answer is NO.

If you want to handle mouse events on different levels such as if you want one MouseArea to handle clicked signals and the other to handle pressAndHold, or if you want one MouseArea to handle clicked most of the time, but pass it through when certain conditions are met following example would help

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
            }
        }
    }
}
Vedanshu
  • 2,230
  • 23
  • 33
  • 2
    agreed. the answer is "no, it can't be done". Bearing this in mind, It would be nice to have a `propagatgeAllEvents` or something similar. +1 for the advice about other event of propagation, so accepting this. – jkj yuio Feb 28 '17 at 16:35