Important: Please read the Qt Code of Conduct -

Block mouse click if window didn't have focus before click occurred

  • I have a QMainWindow with an eventFilter for one of its child widgets where I detect single click (MouseButtonRelease) and do stuff. But I want to do nothing if the window didn't have focus before the click occurred (i. e. I clicked the window to switch to it, not to launch stuff). Is there a way to construct such a condition? So far I don't see a way: by the time the event is processed the window already has focus so isActiveWindow always returns true. And QMainWindow::hasFocus is always false.

  • Moderators

    You should get a QEvent::WindowActivate event, but I'm not quite sure it's a good idea to depend on the order of events ... that is if you can be sure that the activate event arrives before the mouse event.

    Kind regards.

  • @kshegunov
    I know about WindowActivate event, but how can I use it to achieve my goal? How do I know that this MouseButtonRelease event is what caused the window to gain focus?

  • Moderators

    short answer: you can't. At least not using Qt only.
    The OS handles and sends the window activation to the window. Then Qt translates the event and posts it in the event queue.
    So you see this OS specific and you wont be able to determine the correlation between the window activation and mouse button press.

Log in to reply