6

I would like to move the cursor to a given x,y coordinates of the MapCanvas.

Is there an easy way to do this using PyQGIS?

4

You can move cursor like that:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

cursor = QCursor()
cursor.setPos(100, 200)

It moves your cursor to point 100,200 in your screen. If you want to get coordinate of a point on the map canvas, you need to translate them:

# coordinates of point on map canvas
point = QPoint(100,100)
# translate widget coordinates to global screen coordinates
global_point = iface.mapCanvas().mapToGlobal(QPoint(100,100))
# set cursor to these coords
cursor.setPos(global_point.x(), global_point.y())
  • 1
    +1, I spent a little time trying to find this but couldn't. Also great that you added a translation for the map canvas coordinates =) – Joseph Jul 21 '15 at 9:36
  • mapToGlobal(), eh? how cool is that!!! answers my question too: gis.stackexchange.com/questions/217164/… – Jochen Schwarze Nov 10 '16 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.