Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt qml. Can a MouseArea see events, but pass them all to parent without affecting them?

Tags:

qt

qml

qtquick2

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,

like image 464
jkj yuio Avatar asked Feb 28 '17 10:02

jkj yuio


1 Answers

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
            }
        }
    }
}
like image 131
Vedanshu Avatar answered Oct 19 '22 04:10

Vedanshu