[Interest] Does QML have a way to detect if a click is outside an element?

Mark Gaiser markg85 at gmail.com
Fri Mar 5 14:13:54 CET 2021


On Fri, Mar 5, 2021 at 11:40 AM Mitch Curtis <mitch.curtis at qt.io> wrote:

> > -----Original Message-----
> > From: Interest <interest-bounces at qt-project.org> On Behalf Of Mark
> Gaiser
> > Sent: Friday, 5 March 2021 10:39 AM
> > To: Qt Interest <interest at qt-project.org>
> > Subject: [Interest] Does QML have a way to detect if a click is outside
> an
> > element?
> >
> > Hi,
> >
> > Picture a QML window with a red rectangle in the middle of it (say
> 200x200
> > px).
> > The window itself is, say, 500x500 px.
> >
> > Now I'm looking for a way to detect when you click on any area outside
> the
> > red rectangle but inside the white window area.
> >
> > The reason I need this is to provide a more intuitive interaction. In my
> > particular case the red rectangle is a calendar popup that needs to go
> away
> > when you click outside of it but inside the window still.
> >
> > I can imagine 2 possible solutions, both are quite cumbersome to
> implement.
> >
> > 1. One can make a kind of "EventFilter" object in C++ and expose it to
> QML.
> > So that custom event filters can be made which would allow me to find a
> click
> > that occurs outside the rectangle.
> >
> > 2. I can make an application with overlapping MouseArea{} and figure it
> out
> > that way. But this is really hacky and very error prone.
> >
> > Does QML provide another more native way for this kind of interaction?
>
> Popup provides this functionality:
>
> https://doc.qt.io/qt-5/qml-qtquick-controls2-popup.html
>
> Hi Mitch,

Thank you! That actually worked! :)
I'm surprised because the code where i used this uses QtQuick.Controls 1
and 2, which smells scary. But hey, it works.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210305/dcaaadb9/attachment.html>


More information about the Interest mailing list