[GIS] way to take control of the mouse pointer using python in QGIS

mouse positionpyqgispythonqgis

I would like to take control of the mouse pointer in QGIS (for example constraining it to a given extent…) Is there any way to do that using PyQGIS?

Best Answer

This standalone python snippet uses the setPos method of QCursor to move my mouse pointer to the top left corner of my screen:

 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 app = QApplication([])
 cur = QCursor()
 cur.setPos(0,0)

(Note this is standalone python, not QGIS python. But the last two lines should work in a QGIS python console.)

You could then constrain it to an extent by trapping mouse events and resetting. You need a good understanding of Qt (the underlying user interface library of QGIS) to do this well. Search for "qt move mouse pointer" and similar for much enlightenment.