Results 1 to 4 of 4

Thread: Mouse position on screen

  1. #1
    Join Date
    Apr 2008
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Mouse position on screen

    In my application I have to do like a drawing/painting of a custom polygon. For this purpose as long as the mouse button is pressed and the mouse is moving, the points of the mouse should be saved. After releasing the button the polygon will be painted.
    The problem is that the mouse can go over the applications window. This points should be stored as well in order to shrink the whole polygon afterwards to fit in the window.

    Now my question:
    How can I get the mouse position on the whole screen? And how can I find out wheter the mouse button was released outside the applications window?
    Or is there something like a global event filter?

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

    Default Re: Mouse position on screen

    Experiment with this test app:
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. class Widget : public QWidget
    5. {
    6. protected:
    7. void mousePressEvent(QMouseEvent* event)
    8. {
    9. qDebug() << event << event->pos();
    10. }
    11.  
    12. void mouseMoveEvent(QMouseEvent* event)
    13. {
    14. qDebug() << event << event->pos();
    15. }
    16.  
    17. void mouseReleaseEvent(QMouseEvent* event)
    18. {
    19. qDebug() << event << event->pos();
    20. }
    21. };
    22.  
    23. int main(int argc, char *argv[])
    24. {
    25. QApplication app(argc, argv);
    26. Widget window;
    27. window.show();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Mouse position on screen

    Depending on platform you can create filters for the native messages. e.g. winEvent on windows. I know you can register to find keypresses in the win32 api, regardless of what application or what has focus, so I suspect you can do the same with mouse events.

    Although that makes you a bit platform-dependent if you don't write routines for all platforms.

  4. #4

    Question Re: Mouse position on screen

    Hello,

    I've a similar problem.

    I know this is not that much Qt-related but in the end a Qt-Application will need that stuff.

    I'm searching for a way to get the absolute mouse coordinates of every ButtonPress on the desktop. With 'every' I mean that a click can also occur on the desktop itself or on another application/window.

    I was told that's not possible with Qt so I played around with xlib (since it's for unix/linux). With xlib I found a way to get the absolute coordinates if a ButtonPress occurs, but only if it occurs in the self made window:

    Qt Code:
    1. #include <X11/Xlib.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <string.h>
    5.  
    6. /*
    7.  * This program prints the absolute coordinates of the
    8.  * mouse if you press a mouse button.
    9.  *
    10.  * This works only if you click IN THE WINDOW. I want to
    11.  * have it independent of the window. That is, if there
    12.  * is anywhere (other windows) a button press, I want the
    13.  * mouse coordinates, too.
    14.  *
    15.  * Any hint?
    16.  */
    17.  
    18. int main() {
    19. Display *display;
    20. int screen;
    21. Window window;
    22. XEvent event;
    23.  
    24. Window QueryRoot, QueryChild;
    25. int AbsoluteX, AbsoluteY;
    26. int RelativeX, RelativeY;
    27. unsigned int ModKeyMask;
    28.  
    29. /* open connection with the server */
    30. display = XOpenDisplay(NULL);
    31. if (display == NULL) {
    32. printf("Cannot open display\n");
    33. exit(1);
    34. }
    35. screen = DefaultScreen(display);
    36.  
    37. /* create window */
    38. window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 200, 200, 1,
    39. BlackPixel(display, screen), WhitePixel(display, screen));
    40.  
    41. /* select kind of events we are interested in */
    42. XSelectInput(display, window, ExposureMask | ButtonPressMask);
    43. /*XSelectInput(display, RootWindow(display, screen), ExposureMask | ButtonPressMask);*/
    44.  
    45. /* map (show) the window */
    46. XMapWindow(display, window);
    47.  
    48. /* event loop */
    49. while (1) {
    50. XNextEvent(display, &event);
    51. if (event.type == ButtonPress) {
    52. printf("Button was pressed!\n");
    53. XQueryPointer(display, window, &QueryRoot, &QueryChild, &AbsoluteX,
    54. &AbsoluteY, &RelativeX, &RelativeY, &ModKeyMask);
    55. printf("AbsoluteX: %d\n", AbsoluteX);
    56. printf("AbsoluteY: %d\n", AbsoluteY);
    57. }
    58. }
    59.  
    60. /* close connection to server */
    61. XCloseDisplay(display);
    62. return 0;
    63. }
    To copy to clipboard, switch view to plain text mode 

    Any hint how to accomplish this on x11?

    And let's say that there is a way to do this...how to integrate this x11 stuff into a Qt-Application (since Qt uses xlib itself)?!

Similar Threads

  1. grabbing position of mouse
    By safknw in forum Qt Programming
    Replies: 10
    Last Post: 28th June 2008, 15:00
  2. Setting manually cursor position on multi screen systems
    By irreriwan in forum Qt Programming
    Replies: 0
    Last Post: 4th March 2008, 09:47
  3. hiding mouse on second screen
    By safknw in forum KDE Forum
    Replies: 1
    Last Post: 24th November 2006, 11:09
  4. Qt4.1 Mouse Position & Scaling
    By Paul Drummond in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 19:02
  5. Clearing a QLabel since the mouse position
    By SkripT in forum Qt Programming
    Replies: 10
    Last Post: 18th January 2006, 18:23

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.