Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to detect mouse click outside of widget?



  • I've created a modal QDialog which shows a single QLineEdit. I'm using it to allow the user to change the name of a data object. I'd like to automatcially close my dialog with a call to reject() if the user ever clicks outside of the dialog area. I've added a mousePressEvent() to my dialog's class, but it never gets called.

    Is there a way for me to tell if the user clicks away from my dialog box?


  • Moderators

    @kitfox said in How to detect mouse click outside of widget?:

    Is there a way for me to tell if the user clicks away from my dialog box?

    either listen to a QEvent::WindowDeactivate event or use qApp->installEventFilter(filter) and listen for mouse press events



  • Alternatively, you can use grabMouse() to receive mouse events. However, that will cause the first click outside your dialog (the one where you close the dialog) to be eaten up.


Log in to reply