Is it possible to "intercept" a modal dialog's event loop?
-
I have had a long discussion in https://forum.qt.io/topic/88284/gui-event-blocking. Please don't think me mad/obstreperous, but at least for my own benefit I'd like to ask whether something very specific is or is not possible in Qt.
- Suppose I have displayed a modal
QDialog
, and calledQDialog.exec()
. - Suppose user has clicked a button there, and that invokes other code (the dialog is still displayed).
- Suppose that code (completely outside of the dialog's code) wishes to intercept the dialog's event loop (in order to discard certain events before the dialog gets to see them, while allowing other events to be processed normally).
Note that the calling dialog's code must have no knowledge of this behaviour in the invoked code. the solution must be 100% inside the invoked code.
Is this possible to achieve in Qt's architecture, or not?
I see two possible solutions which, if they work, would be acceptable.
Solution 1:
The called code discovers there is a modal dialog being displayed, somehow (e.g. there must be a Qt call to discover the dialog currently being displayed?). From that dialog handle, the called code can dynamically "intercept" its event loop/filter and remove certain events while allowing others through untouched.
Solution 2:
When a modal dialog is displayed, does Qt top-level/application event filter still get called before an event is passed onto the dialog's event handler, such that an event could be removed?
EDIT In that other thread, @kshegunov has just posted something which might be what I'm asking about in Solution #2:
You could install that kind of class directly on the QCoreApplication object (with the proper modification), where you'd filter the events globally
If the answer is no, this cannot be done, I will understand and accept that!
Thanks in advance.
- Suppose I have displayed a modal
-
Hi,
Can you give a more concrete example about that ?
You might also be interested in QDialog::open rather than exec
-
Hi,
Can you give a more concrete example about that ?
You might also be interested in QDialog::open rather than exec
@SGaist
The full, concrete example is described in detail in https://forum.qt.io/topic/88284/gui-event-blocking. I have posted this new thread as that was getting out of hand, and has now boiled down to the specific question I am posing here.For whatever reason, and for right or for wrong, which I'm hoping not to get into here, the question remains: without any change of code in the dialog (including, it is invoked via
exec()
and notopen()
), and without sub-classing the dialog, is it or is it not possible for a completely separate piece of code to filter out (UI) events which would have gone to the dialog? Are either of my Solutions #1 or #2 possible in Qt, or are they not? (For example, solution #2, is the application event loop called, and can remove events, before they are redirected to the current modal dialog?) -
@Pablo-J.-Rogina
Thank you, but I don't think I see any relevance.The question is: can a completely unrelated piece of code "intercept" a modal dialog's event loop, and filter out events before the dialog sees/acts on them? Perhaps via one of the two solutions I have proposed, or perhaps by a way I haven't thought of. Whichever, the solution must not require a single change in the code of the dialog, period.
-
@Pablo-J.-Rogina
Thank you, but I don't think I see any relevance.The question is: can a completely unrelated piece of code "intercept" a modal dialog's event loop, and filter out events before the dialog sees/acts on them? Perhaps via one of the two solutions I have proposed, or perhaps by a way I haven't thought of. Whichever, the solution must not require a single change in the code of the dialog, period.
# 1
Seems improbable bordering impossible.# 2
Yes, this is possible. Installing an event filter on the dialog or globally on the application object allows you to filter out events before they reach their destination. EveryQObject
can accept an event filter, and the event filter object'seventFilter
method is called before the events are propagated to the receiving object. -
# 1
Seems improbable bordering impossible.# 2
Yes, this is possible. Installing an event filter on the dialog or globally on the application object allows you to filter out events before they reach their destination. EveryQObject
can accept an event filter, and the event filter object'seventFilter
method is called before the events are propagated to the receiving object.@kshegunov
Thanks so much for addressing my two solutions! I will have a go at #2 approach when I have some time....