[Qt-interest] QCursor::setPos causes cursor to "stick" for a short time
Tom Haynes
trhaynes at gmail.com
Wed Aug 5 17:47:10 CEST 2009
Note: my code is PyQt but I have no reason to believe that this is a
Python (or wrapping) problem.
I'm using a QTimer to get the current cursor position every x
milliseconds. Every n runs of this, it resets the cursor to the middle
of the screen using the static QCursor::setPos(int x, int y). However,
queries on the position of the cursor for around the next 200ms show
it being in the middle of the screen, despite me flailing the mouse
around wildly. You can even see it "stick" in the middle for a short
while. Here is some code and output that illustrates this behavior:
---
import sys, os, xvis, time, math
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Driver(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
self.setMouseTracking(True)
self.last_cursor = QCursor.pos()
self.last_update = 0
self.reset_interval = 100
self.middle_x = app.desktop().screenGeometry().width() / 2.0
self.middle_y = app.desktop().screenGeometry().height() / 2.0
def get_cursor_change(self):
"""Return (x,y) tuple of cursor change"""
self.last_update += 1
cursor = QCursor.pos()
diff = cursor - self.last_cursor
self.last_cursor = cursor
if self.last_update > self.reset_interval:
# move cursor to middle of screen
QCursor.setPos(self.middle_x, self.middle_y)
self.last_cursor = QPoint(self.middle_x, self.middle_y)
self.last_update = 0
return (diff.x(), diff.y())
def mouseMoveEvent(self, event):
event.accept()
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.driver = Driver(self)
self.setCentralWidget(self.driver)
# run loop
timer = QTimer(self)
self.connect(timer, SIGNAL("timeout()"), self.update)
timer.start(50)
def update(self):
"""Gets called each iteration"""
cursor_change = self.driver.get_cursor_change()
print cursor_change
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
mainwindow.raise_()
sys.exit(app.exec_())
---
and output:
---
...
(0, 45)
(-56, 63)
(-49, 18)
(-59, -39)
(-11, -98)
(51, -30)
(98, 30)
(44, 68)
(-59, 60)
(-96, 19)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(4, 91)
(-23, 44)
(-54, 3)
(-98, -44)
(-52, -86)
(43, -76)
(63, -3)
(56, 47)
(5, 84)
(-68, 72)
(-83, 18)
(-109, -34)
(-61, -52)
(18, -44)
...
---
you can see the (0, 0)s right after the cursor gets reset.
Anyone have any ideas?
-tom
More information about the Qt-interest-old
mailing list