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

Mitch Curtis mitch.curtis at qt.io
Fri Mar 5 11:40:58 CET 2021


> -----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
 
> Best regards,
> Mark Gaiser


More information about the Interest mailing list