PDA

View Full Version : QCursor::setPos makes cursor inactive for ~200ms



trhaynes
29th July 2009, 21:24
Hi there,

I have some code that uses 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.

Does anyone know why this happens or how I can fix it?

I'm using PyQt with Qt 4.4 right now. Thanks for your help.

-tom

wysota
29th July 2009, 22:01
Can we see some code of yours?

trhaynes
30th July 2009, 15:07
Here is some code that illustrates what I am talking about:


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 here is some sample 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)
...


The above is when moving the mouse around constantly. You can see the (0,0)s occur right after the mouse is reset to the middle.

Thanks for your help.

-tom