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. Block mouse click if window didn't have focus before click occurred
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.8k Views 2 Watching
  • 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by Violet Giraffe
    #1

    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.

    kshegunovK 1 Reply Last reply
    0
    • V Violet Giraffe

      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.

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

      @Violet-Giraffe
      Hi,
      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.

      Read and abide by the Qt Code of Conduct

      V 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @Violet-Giraffe
        Hi,
        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.

        V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

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

        raven-worxR 1 Reply Last reply
        0
        • V Violet Giraffe

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2

          • Login

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