[Qt-qml] Forwarding mouse events to MouseArea underneath

Alan Alpert alan.alpert at nokia.com
Mon Jan 10 03:11:01 CET 2011


On Friday, January 07, 2011 08:54:33 pm ext Gregory Schlomoff wrote:
> > http://doc.qt.nokia.com/4.7-snapshot/qml-mouseevent.html#accepted-prop ?
> 
> Yeah, you'd think that's what this property was made for. And you'd be
> wrong :)
> 
> MouseEvent.accepted seems perfectly useless, but in very specific
> cases, like in onDoubleClicked.
> 
> http://doc.qt.nokia.com/main-snapshot/qml-mousearea.html#onClicked-signal
> --> "The accepted property of the MouseEvent parameter is ignored in
> this handler."
> 
> On Fri, Jan 7, 2011 at 5:50 PM, Juha Turunen <turunen at iki.fi> wrote:
> > http://doc.qt.nokia.com/4.7-snapshot/qml-mouseevent.html#accepted-prop ?
> > 
> > On Fri, Jan 7, 2011 at 11:51 AM, Gregory Schlomoff
> > 
> > <gregory.schlomoff at gmail.com> wrote:
> >> Hello,
> >> 
> >> We are in a situation where we have one big MouseArea on top handling
> >> click events and several smaller MouseAreas below handling
> >> double-click events.
> >> 
> >> Unfortunately, there is currently no way in QML to say that an event
> >> should "bubble" up or down to the next receiver. The top MouseArea
> >> will always catch all events, and the MouseAreas below will get
> >> nothing.
> >> 
> >> There is a very specific solution when the event you want to handle in
> >> the topmost MouseArea is onDrag. In this case, you can use
> >> drag.filterChildren: true to let the topmost MouseArea catch only drag
> >> event and pass other events to MouseAreas beneath. But it won't work
> >> for other events.
> >> 
> >> We devised a ugly hack to work around this, that basically involves
> >> catching the event in the topmost MouseArea and manually calling the
> >> event handler on any MouseArea underneath:
> >> 
> >> ===============
> >> import QtQuick 1.0
> >> 
> >> Rectangle {
> >>        width: 360
> >>        height: 360
> >> 
> >>        MouseArea {
> >>                anchors {fill: parent; margins: 40}
> >>                onClicked: console.log("hello from below");
> >>        }
> >> 
> >>        MouseArea {
> >>                id: mouseArea
> >>                anchors.fill: parent
> >> 
> >>                onClicked: {
> >>                        console.log("hello from top")
> >>                        forwardEvent(mouse, "clicked");
> >>                }
> >> 
> >>                function forwardEvent(event, eventType) {
> >>                        mouseArea.visible = false
> >>                        var item = parent.childAt(event.x, event.y)
> >>                        mouseArea.visible = true
> >>                        if (item && item != mouseArea &&
> >> typeof(item[eventType]) == "function") { item[eventType](event);
> >>                        }
> >>                }
> >>        }
> >> }
> >> ===============
> >> 
> >> What do you think? Are there any better solutions? Should this be a
> >> built-in functionnality in QML so that we can avoid this hack,
> >> especially the fact that we have to hide / show the element to get
> >> elements below. Any feedback is appreciated.
> >> 
> >> Thanks,
> >> 
> >> Greg

Sounds like you want http://bugreports.qt.nokia.com/browse/QTBUG-13007 . 
Currently scheduled for 4.8; you might want to watch or vote for that task, 
and adding a comment with a concrete usecase is always helpful.

-- 
Alan Alpert
Software Engineer
Nokia, Qt Development Frameworks


More information about the Qt-qml mailing list