Results 1 to 6 of 6

Thread: Can Qt4 capture mousePress events on the desktop?

  1. #1
    Join Date
    Sep 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Can Qt4 capture mousePress events on the desktop?

    Hello,

    In Qt4 is there a way to capture mouse events that occur when a user clicks inside another (non Qt) application? Ideally these events would be captured via Qt4 rather than something platform specific, would be full-blown QMouseEvents, and could be captured when the MainWindow is hidden.

    I have played around a bit trying to monitor QDesktopWidget with an event filter, but I have nothing to show for it.

    I appreciate any help anyone can throw my way,
    Mark

  2. #2
    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: Can Qt4 capture mousePress events on the desktop?

    You can enable mouse grabbing. This way you'll receive all mouse events that happen on the desktop. Don't forget to disable grabbing afterwards. The grabbing widget will have to be visible to be able to grab the mouse, but you can probably overcome that by creating a completely transparent widget (for example using setMask()).

  3. #3
    Join Date
    Sep 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Arrow Re: Can Qt4 capture mousePress events on the desktop?

    That was great help! Thanks a ton Wysota.

    I am so close, but not quite there.

    By using grabMouse() I am now able to grab all mouse events, even when the mouse is clicked outside of the QWidget. However, these events are now completely captured by my QWidget and do not pass into the third party application that the user clicks on the desktop.

    I want the user to be able to click a third party application on the desktop (such as Firefox), for my QWidget to receive a corresponding QMouseEvent, but then I want the event to be passed onto the third party application. So that in effect I can store the x,y coordinates of the cursor when a person clicks the minimize button in a third party application without interfering with the user's attempt to minimize the third party application.

    I can think of two possible ways to pass events back out to third party apps:

    1) Qt bubbles the event up out of the QWidget, into Main Window, and out to the third party application. I have played with event->ignore(), but it only seems to help the event bubble out of the current QWidget and into Main Window, but not into a third party application.

    2) If I could create a new OS mouse press event at the x, y coordinates that I picked up with the QMouseEvent from the original user mouse press. It seems that this might be possible with void QWSInputMethod::sendMouseEvent ( const QPoint & position, int state, int wheel ), but this is a QTopia only feature and I need to stick with Qt4.

    Thanks for the help Wysota and I really appreciate any help completing this puzzle,
    Mark

  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: Can Qt4 capture mousePress events on the desktop?

    In general you have to perform this on OS level probably using native events (through QApplication::x11EventFilter or similar).

  5. #5
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can Qt4 capture mousePress events on the desktop?

    Do you solve the problem,can you get me a TIP.

  6. #6
    Join Date
    Feb 2006
    Posts
    31

    Default Re: Can Qt4 capture mousePress events on the desktop?

    you should use native code;

    if on win32, try this SetWindowsHookEx

    if on X11, try
    Qt Code:
    1. Window root = RootWindow(dpy,The_screen);
    2. int buttons = 0;
    3.  
    4. /* Make the target cursor */
    5. cursor = XCreateFontCursor(dpy, /*XC_crosshair*/XC_hand2);
    6.  
    7. /* Grab the pointer using target cursor, letting it room all over */
    8. status = XGrabPointer(dpy, root, False,
    9. ButtonPressMask|ButtonReleaseMask, GrabModeSync,
    10. GrabModeAsync, root, cursor, CurrentTime);
    11. if (status != GrabSuccess) {
    12. return None;
    13. }
    14.  
    15. /* allow one more event */
    16. XAllowEvents(dpy, SyncPointer, CurrentTime);
    17. XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
    18. switch (event.type) {
    19. case ButtonPress:
    20. buttons++;
    21. break;
    22. case ButtonRelease:
    23. if (buttons > 0) /* there may have been some down before we started */
    24. buttons--;
    25. break;
    26. }
    27.  
    28. XUngrabPointer(dpy, CurrentTime); /* Done with pointer */
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 6th January 2009 at 19:33. Reason: missing [code] tags

Similar Threads

  1. Capture events in a custom delegate
    By vfernandez in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2008, 05:33
  2. MousePress Events on ContextMenu items
    By Naveen in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2006, 07:26

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.