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. How to detect mouse click events outside a QDialog widget?
QtWS25 Last Chance

How to detect mouse click events outside a QDialog widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 8.0k 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    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-qt

    I 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!


    Qt Developer

    kshegunovK 1 Reply Last reply
    0
    • A Offline
      A Offline
      AlexMal
      wrote on last edited by AlexMal
      #2

      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.

      1 Reply Last reply
      1
      • X xtingray

        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-qt

        I 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!

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

        Is the dialog native or alien?
        Look at QWidget::grabMouse (look at the notes too).

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xtingray
          wrote on last edited by xtingray
          #4

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


          Qt Developer

          kshegunovK 1 Reply Last reply
          0
          • F Offline
            F Offline
            Fuel
            wrote on last edited by
            #5

            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()

            1 Reply Last reply
            0
            • X xtingray

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

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

              @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 at QWidget::focusOutEvent) for your root widget (usually that'd be your QMainWindow). I'm speculating though, as I haven't tried it.

              Read and abide by the Qt Code of Conduct

              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