Results 1 to 4 of 4

Thread: Cannot update mouse Pointer position

  1. #1
    Join Date
    Aug 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Cannot update mouse Pointer position

    Hi,

    I have modified scribble example (found at following link) code to capture leaveEvent and keyPressEvent.

    http://doc.trolltech.com/4.5/widgets-scribble.html

    And whenever this event occurs, within this event handler i am doing QCursor::setPos(x, y) to reposition my mouse pointer to within mainwindow boundary area. And inside keyPressEvent handler i am moving the cursor by 1 pixel when ever the direction keys are pressed. Whenever the leaveEvent occurs because of the keypresses made to move out of the boundary, I am unable to set mouse Pointer position in the same event iteration. However i have to press one extra key to update the mouse pointer.

    Also though i have configured setMouseTracking(true) in my constructor, I am unable to get MouseMove Events.

    Am i missing something trivial here ?
    Last edited by srinirao; 2nd November 2009 at 09:05.

  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: Cannot update mouse Pointer position

    We'd have to see some code.
    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.


  3. #3
    Join Date
    Aug 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Cannot update mouse Pointer position

    My intention is to mimic Gtk example : http://gtkforums.com/about182.html

    I have re-implemented leavemouse and key press event handler in mainwindow class as follows. My intention is to get the mouse pointer move on key presses. But when it hits the boundary the leavemouse event should re-position itself to within the window by adjusting few pixels. I event tried generating mouseMoveEvent from within leave. But it is just creating another mouse move event. I am also unable to get contineous mouseMove event when i re-implement it in MainWindow class.

    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *ke)
    2. {
    3. int dx = 0;
    4. int dy = 0;
    5. int offset = 1;
    6. printf("%s: Got key %d\n", __FUNCTION__, ke->key());
    7. if ( ke->isAutoRepeat() )
    8. offset = 5;
    9. if ( ke->key() == Qt::Key_Left )
    10. dx = -offset;
    11. else if ( ke->key() == Qt::Key_Right)
    12. dx = offset;
    13. else if ( ke->key() == Qt::Key_Up)
    14. dy = -offset;
    15. else if ( ke->key() == Qt::Key_Down)
    16. dy = offset;
    17. else if ( ke->key() == Qt::Key_Q)
    18. {
    19. printf("%s: Got Quit key \n", __FUNCTION__);
    20. exit(1);
    21. }
    22. if ( dx != 0 || dy != 0 )
    23. {
    24. const QRect rect = scribbleArea->contentsRect();
    25. const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
    26. printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
    27. rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
    28. printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
    29. pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
    30. int x = pos.x() + dx;
    31. int y = pos.y() + dy;
    32. #if 1
    33. QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
    34. update();
    35. #else
    36. QMouseEvent event1(QEvent::MouseMove, QPoint(x,y), (Qt::MouseButton)0, (Qt::MouseButtons)0, 0);
    37. QApplication::sendEvent(this, &event1);
    38. #endif
    39. }
    40. }
    41. void MainWindow::leaveEvent(QEvent *event)
    42. {
    43. const QRect rect = scribbleArea->contentsRect();
    44. const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
    45. /*
    46.   printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
    47.   rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
    48.   printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
    49.   pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
    50.   */
    51. int x = pos.x();
    52. int y = pos.y();
    53. if(pos.y() < rect.top())
    54. {
    55. printf("%s: Moving Up \n", __FUNCTION__);
    56. y = rect.top() + 1;
    57. }
    58. else if(pos.y() > rect.bottom())
    59. {
    60. printf("%s: Moving Down \n", __FUNCTION__);
    61. y = rect.bottom() -1;
    62. }
    63. else if(pos.x() < rect.left())
    64. {
    65. printf("%s: Moving Left \n", __FUNCTION__);
    66. x = rect.left() + 1;
    67. }
    68. else if(pos.x() > rect.right())
    69. {
    70. printf("%s: Moving Right \n", __FUNCTION__);
    71. x = rect.right() - 1;
    72. }
    73. else
    74. {
    75. printf("%s: Invalid Leave \n", __FUNCTION__);
    76. }
    77. #if 0
    78. QCursor::setPos(scribbleArea->mapToGlobal(QPoint(100, 100)));
    79. #else
    80. QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
    81. #endif
    82. QApplication::processEvents();
    83. }
    84. srinirao@srinirao:~> wc -l mainwindow.cpp
    85. 83 mainwindow.cpp
    86. srinirao@srinirao:~> cat mainwindow.cpp
    87. void MainWindow::keyPressEvent(QKeyEvent *ke)
    88. {
    89. int dx = 0;
    90. int dy = 0;
    91. int offset = 1;
    92. printf("%s: Got key %d\n", __FUNCTION__, ke->key());
    93. if ( ke->isAutoRepeat() )
    94. offset = 5;
    95. if ( ke->key() == Qt::Key_Left )
    96. dx = -offset;
    97. else if ( ke->key() == Qt::Key_Right)
    98. dx = offset;
    99. else if ( ke->key() == Qt::Key_Up)
    100. dy = -offset;
    101. else if ( ke->key() == Qt::Key_Down)
    102. dy = offset;
    103. else if ( ke->key() == Qt::Key_Q)
    104. {
    105. printf("%s: Got Quit key \n", __FUNCTION__);
    106. exit(1);
    107. }
    108. if ( dx != 0 || dy != 0 )
    109. {
    110. const QRect rect = scribbleArea->contentsRect();
    111. const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
    112. printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
    113. rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
    114. printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
    115. pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
    116. int x = pos.x() + dx;
    117. int y = pos.y() + dy;
    118. #if 1
    119. QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
    120. update();
    121. #else
    122. QMouseEvent event1(QEvent::MouseMove, QPoint(x,y), (Qt::MouseButton)0, (Qt::MouseButtons)0, 0);
    123. QApplication::sendEvent(this, &event1);
    124. #endif
    125. }
    126. }
    127. void MainWindow::leaveEvent(QEvent *event)
    128. {
    129. const QRect rect = scribbleArea->contentsRect();
    130. const QPoint pos = scribbleArea->mapFromGlobal(QCursor::pos());
    131. /*
    132.   printf("%s: Got Rect as (%d,%d), top=%d, bottom=%d, width=%d, height=%d \n", __FUNCTION__,
    133.   rect.x(), rect.y(), rect.top(), rect.bottom(), rect.width(), rect.height());
    134.   printf("%s: Got position as global(%d,%d), QCursor(%d,%d) \n", __FUNCTION__,
    135.   pos.x(), pos.y(), QCursor::pos().x(), QCursor::pos().y());
    136.   */
    137. int x = pos.x();
    138. int y = pos.y();
    139. if(pos.y() < rect.top())
    140. {
    141. printf("%s: Moving Up \n", __FUNCTION__);
    142. y = rect.top() + 1;
    143. }
    144. else if(pos.y() > rect.bottom())
    145. {
    146. printf("%s: Moving Down \n", __FUNCTION__);
    147. y = rect.bottom() -1;
    148. }
    149. else if(pos.x() < rect.left())
    150. {
    151. printf("%s: Moving Left \n", __FUNCTION__);
    152. x = rect.left() + 1;
    153. }
    154. else if(pos.x() > rect.right())
    155. {
    156. printf("%s: Moving Right \n", __FUNCTION__);
    157. x = rect.right() - 1;
    158. }
    159. else
    160. {
    161. printf("%s: Invalid Leave \n", __FUNCTION__);
    162. }
    163. #if 0
    164. QCursor::setPos(scribbleArea->mapToGlobal(QPoint(100, 100)));
    165. #else
    166. QCursor::setPos(scribbleArea->mapToGlobal(QPoint(x, y)));
    167. #endif
    168. QApplication::processEvents();
    169. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by srinirao; 5th November 2009 at 07:25. Reason: Gtk Example pointer added

  4. #4
    Join Date
    Aug 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Cannot update mouse Pointer position

    I found something here, The QCursor::setPos() is not working for me in my platform. Let us say i will set the cursor position to some fixed co-ordinates, it doesnot position the cursor to that point. But the same example seems to work fine on i386 machine.

    Qt Code:
    1. QCursor::setPos(mapToGlobal(QPoint(100, 100)));
    To copy to clipboard, switch view to plain text mode 


    after following the code, it is finally calling :

    Qt Code:
    1. void QWSDisplay::setCursorPosition(int x, int y)
    To copy to clipboard, switch view to plain text mode 

    This does not seem to be working.

Similar Threads

  1. Word at mouse click position in QGraphicsTextItem
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2008, 04:56
  2. free mouse pointer
    By raflegan in forum Qt Programming
    Replies: 1
    Last Post: 1st October 2008, 14:11
  3. Mouse position on screen
    By Nippler in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2008, 14:22
  4. Qt4.1 Mouse Position & Scaling
    By Paul Drummond in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 19:02

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.