How to detect mouse click events outside a QDialog widget?
-
Hi,
Googling for a solution to my requirement, I found these interesting resources:
https://stackoverflow.com/questions/16407170/detecting-click-outside-qwidget
https://stackoverflow.com/questions/14147304/how-to-detect-that-my-application-lost-focus-in-qtI would like to validate what is the best approach to detect a click event outside an active QDialog instance? (focus event, active window event, etc).
Any help will be appreciated. Thanks!
-
Widget only may track events which occur within it. It receives events when it lose/gets focus or mouse leaves/enters, but it will not receive event occurring outside of it unless it is notified by the widget which received such events explicitly.
The examples you mentioned only discuss interception of leave event or losing focus event in subclass or by installing event filter.
-
Hi,
Googling for a solution to my requirement, I found these interesting resources:
https://stackoverflow.com/questions/16407170/detecting-click-outside-qwidget
https://stackoverflow.com/questions/14147304/how-to-detect-that-my-application-lost-focus-in-qtI would like to validate what is the best approach to detect a click event outside an active QDialog instance? (focus event, active window event, etc).
Any help will be appreciated. Thanks!
Is the dialog native or alien?
Look at QWidget::grabMouse (look at the notes too). -
@kshegunov Sorry, I was looking for the definition of "alien" related to a Qt widget, but I couldn't find it. I would appreciate if you can explain it to me. Thanks.
PS: The reason I am trying to capture the event outside the dialog is because in several touch surfaces, when a dialog shows up, users tend to touch outside the widget to close it. This is some kind of standard coming from mobile devices that I would like to support as an usability feature. Do you think this is possible to implement?
-
Looks like you need to catch the Mouse Click with your Dialog, but you can catch the Position of the Cursor on the Screen. So you can get the Position and when its outside of your Dialog you can do something with it. I mean with QApplication::desktop()->cursor()
-
@kshegunov Sorry, I was looking for the definition of "alien" related to a Qt widget, but I couldn't find it. I would appreciate if you can explain it to me. Thanks.
PS: The reason I am trying to capture the event outside the dialog is because in several touch surfaces, when a dialog shows up, users tend to touch outside the widget to close it. This is some kind of standard coming from mobile devices that I would like to support as an usability feature. Do you think this is possible to implement?
@xtingray said in How to detect mouse click events outside a QDialog widget?:
Sorry, I was looking for the definition of "alien" related to a Qt widget, but I couldn't find it. I would appreciate if you can explain it to me.
http://doc.qt.io/qt-5/qwidget.html#native-widgets-vs-alien-widgets
In short: widgets that get a parent widget will not (ordinarily) get a window handle from the underlying window manager - those are alien widgets. They paint onto the root widget's window handle directly. The one that don't have a parent are native - they get a window handle from the manager.
Do you think this is possible to implement?
It's possible with
grabMouse
on Linux, I can't tell for other OSes though. On the other hand you can try to do some nifty handling of the lose focus event (look atQWidget::focusOutEvent
) for your root widget (usually that'd be yourQMainWindow
). I'm speculating though, as I haven't tried it.