Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it possible to "intercept" a modal dialog's event loop?
Forum Update on Monday, May 27th 2025

Is it possible to "intercept" a modal dialog's event loop?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #1

    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.

    1. Suppose I have displayed a modal QDialog, and called QDialog.exec().
    2. Suppose user has clicked a button there, and that invokes other code (the dialog is still displayed).
    3. 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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you give a more concrete example about that ?

      You might also be interested in QDialog::open rather than exec

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you give a more concrete example about that ?

        You might also be interested in QDialog::open rather than exec

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @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 not open()), 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?)

        1 Reply Last reply
        0
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @JonB could this example be helpful to you? It's a dialog showing progress of a long running task in the background. I don't think that you need to mess up with event loops, having the GUI and threading should suffice to my opinion.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          JonBJ 1 Reply Last reply
          1
          • Pablo J. RoginaP Pablo J. Rogina

            @JonB could this example be helpful to you? It's a dialog showing progress of a long running task in the background. I don't think that you need to mess up with event loops, having the GUI and threading should suffice to my opinion.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @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.

            kshegunovK 1 Reply Last reply
            0
            • Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @JonB I was thinking about your "old" post

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • JonBJ JonB

                @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.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                # 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. Every QObject can accept an event filter, and the event filter object's eventFilter method is called before the events are propagated to the receiving object.

                Read and abide by the Qt Code of Conduct

                JonBJ 1 Reply Last reply
                3
                • kshegunovK kshegunov

                  # 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. Every QObject can accept an event filter, and the event filter object's eventFilter method is called before the events are propagated to the receiving object.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @kshegunov
                  Thanks so much for addressing my two solutions! I will have a go at #2 approach when I have some time....

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved