Results 1 to 12 of 12

Thread: How to get mouse click events outside the Qt window?

  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to get mouse click events outside the Qt window?

    I have a Qt application that always stays on top. Now, i want to hide the window if the user clicks a specific area outside the Qt window. So, if user clicks outside the Qt window and outside the specific coordinates, nothing should happen and if he clicks outside the window but inside the specific coordinates the Qt window should hide. I have this code as of now:

    Qt Code:
    1. if (event->type() == QEvent::FocusOut)
    2. {
    3. qDebug("focus lost");
    4. return true;
    5. }
    6. else if(event->type() == QEvent::MouseButtonPress)
    7. {
    8. QPoint pos = dynamic_cast<QMouseEvent*>(event)->pos();
    9. qDebug() << "mouse click position=" << mapToGlobal(pos) << "global=" << dynamic_cast<QMouseEvent*>(event)->globalPos();
    10. return false;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Using the above code, i get the focus lost event when user clicks outside the Qt window but i don't get the coordinates of the click event. QEvent::MouseButtonPress case is executed only when user clicks the mouse inside the Qt window. Is there any way of getting the global mouse coordinates when user clicks outside the Qt window?

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to get mouse click events outside the Qt window?

    I haven't tested, but I think that's what QWidget::grabMouse() is for.

  3. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get mouse click events outside the Qt window?

    grabMouse is for grabbing the mouse. I don't want to grab the mouse, i want to get the mouse cursor position when user clicks outside the Qt window i.e. on lost focus event. Is there any way to get this in Qt or do i need to call an X11 API for the same?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get mouse click events outside the Qt window?

    Reimplement QApplication::x11EventFilter() and install an event filter on the event dispatcher as explained in the docs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get mouse click events outside the Qt window?

    Actually i got the mouse position outside the Qt window using the following:

    Qt Code:
    1. if (event->type() == QEvent::FocusOut)
    2. {
    3. qDebug("focus lost");
    4. QPoint p=QCursor::pos();
    5. qDebug() << "mouse position=" << p;
    6. if ((p.x() >= 100 && p.x() <= 300) && (p.y() >= 100 && p.x() <= 700))
    7. {
    8. qDebug("hiding window");
    9. hide();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    So, i am able to hide my window if user clicks outside the window. The problem now is that if user clicks outside the window and outside the area i defined, i get focus lost event and the window is not hidden. Now, if user clicks inside the defined area, i don't get any focus out event since the focus was already lost earlier when user clicked outside the window.

    So, the above code works only if the Qt window already has focus. Is there any way to force the focus back to the Qt window when i get focus out event? I tried grabKeyboard but it only grabs the keyboard, it doesn't make the Qt window grab focus.

    Is it possible to use x11EventFilter() for forcing the Qt application not to loose focus?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get mouse click events outside the Qt window?

    This code will not work in most of the cases. If you want to test it, position the mouse cursor over your widget and press tab. Now do the same but position the mouse cusor inside your special area. After pressing tab the window will get hidden in the second case.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get mouse click events outside the Qt window?

    Quote Originally Posted by wysota View Post
    This code will not work in most of the cases. If you want to test it, position the mouse cursor over your widget and press tab. Now do the same but position the mouse cusor inside your special area. After pressing tab the window will get hidden in the second case.
    No, the code i posted in working fine as i grab the keyboard even after the window looses focus. My main problem is that i want my Qt window never to loose focus.
    Is there any way i can specify that my Qt window never looses focus (similar to Always On Top scenario). If it's not possible in Qt, can i use any X11 APIs to force focus on my Qt window?

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get mouse click events outside the Qt window?

    For Always on top scenario, you can have a look at Qt::WindowStaysOnTopHint (QWidget::setWindowFlags)

  9. #9
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get mouse click events outside the Qt window?

    oh no i was just giving an example for always on top.
    Mainly i want my applicatino never to loose focus but i don't know how to do it.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get mouse click events outside the Qt window?

    Make it fullscreen or the only application using the X server.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get mouse click events outside the Qt window?

    try this.
    first actived current widget
    then
    overload the event function
    Qt Code:
    1. bool YourWidget::event ( QEvent * event )
    2. {
    3. if (event->type() == QEvent::ActivationChange)
    4. {
    5. if(QApplication::activeWindow() != this)
    6. {
    7. this->hide();
    8. }
    9. }
    10. return QWidget::event(event);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th September 2010 at 08:36.

  12. #12
    Join Date
    May 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get mouse click events outside the Qt window?

    maybe:

    void QWidget::leaveEvent(QEvent * event)
    {
    close();
    }

Similar Threads

  1. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 16:03
  2. Replies: 11
    Last Post: 15th July 2008, 14:11
  3. Replies: 1
    Last Post: 9th February 2007, 10:41
  4. Replies: 2
    Last Post: 24th July 2006, 19:36
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 20:25

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.