Results 1 to 3 of 3

Thread: Ignore mouse click on window activation

  1. #1
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Ignore mouse click on window activation

    I have an application with a painting area (QGraphicsView). The user can add a graphics object to the area by clicking on it.

    If the application window is inactive, the user can click on the main window to activate it. Just in this case, I'd like to ignore the MouseButtonPress event in QGraphicsView, so that no object will be added. So the desired behaviour is:

    • Click on painting area of inactive window: Activate window
    • Click on painting area of active window: Add graphics object


    Using an event filter, I got the following order of events when clicking on an inactive window:

    • ApplicationActivated @ app
    • WindowActivate @ all widgets
    • MouseButtonPress @ QGraphicsView
    • MouseButtonRelease @ QGraphicsView


    My first idea was to ignore the first MouseButtonPress event after a WindowActivated event. But this will also ignore the first click when the windows has been activated using the keyboard (e.g. Alt-Tab on Windows).

    Any suggestions are welcome.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Ignore mouse click on window activation

    This is a bit hackish but could work at least on some platforms. When GV receives QEvent::WindowActivate, either
    • post (NOT send) an event to itself
    • start a single shot timer with zero interval
    • use signals and slots with Qt::QueuedConnection
    • use QMetaObject::invokeMethod() with Qt::QueuedConnection
    • in any case, let the the execution return to the event loop.

    If the custom event is received (or the slot is called) before receiving a mouse press event, the window was first activated by tab and then pressed. If a mouse press event is received before the custom event (or the slot getting called), the window was directly activated by mouse press.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    shentian (22nd June 2009)

  4. #3
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Ignore mouse click on window activation

    Thanks a lot for your advice, now it was quite easy.

    As you suggested, I post an event when I recieve WindowActivate, and I ignore the mouse release if my custom event occurs between mouse press and mouse release.

    If the window is activated by a mouse click, the events are:
    1. WindowActivate (post ActivatedUserEvent here)
    2. MouseButtonPressed (already in Queue)
    3. ActivatedUserEvent (my custom event)
    4. MouseButtonReleased

    If the window is activated by Tab, the events are
    1. WindowActivate (post ActivatedUserEvent here)
    2. ActivatedUserEvent (my custom event)
    3. MouseButtonPressed
    4. MouseButtonReleased


    The code looks like this:

    Qt Code:
    1. bool MyGraphicsView::event(QEvent* event)
    2. {
    3. switch (event->type())
    4. {
    5. case QEvent::WindowActivate:
    6. QCoreApplication::postEvent(this, new QEvent(ActivatedUserEvent));
    7. break;
    8. case Private::ActivatedUserEvent:
    9. ignoreNextMouseRelease = true;
    10. break;
    11. default:
    12. break;
    13. }
    14. return QGraphicsView::event(event);
    15. }
    16.  
    17. void MyGraphicsView::mousePressEvent(QMouseEvent* event)
    18. {
    19. QGraphicsView::mousePressEvent(event);
    20. ignoreNextMouseRelease = false;
    21. }
    22.  
    23. void MyGraphicsView::mouseReleaseEvent(QMouseEvent* event)
    24. {
    25. if (ignoreNextMouseRelease)
    26. {
    27. ignoreNextMouseRelease = false;
    28. return;
    29. }
    30. QGraphicsView::mouseReleaseEvent(event);
    31. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to get mouse click events outside the Qt window?
    By montylee in forum Qt Programming
    Replies: 11
    Last Post: 13th July 2015, 21:55
  2. Replies: 11
    Last Post: 15th July 2008, 13:11
  3. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05
  4. Replies: 1
    Last Post: 9th February 2007, 09:41
  5. Replies: 2
    Last Post: 24th July 2006, 18:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.