3

I noticed that PyQGIS has QgsMapToolPan() and QgsMapToolZoom() functions, and the cursor shape on the canvas will change to a hand (for pan), magnifying-glass(+/-) (for zoomIn / zoomOut).

I have a toolbar for Select (arrow icon), Marquee (bounding box icon), besides the Pan, ZoomIn and ZoomOut tool buttons.

Currently, I created the Select & Marquee tools as QgsMapTool(canvas) and setAction with the appropriately UI widget buttons.

How can I change the cursor shape to an arrow when the Select tool is activated, and cross when the Marquee tool is activated?

(I noted the default cursor shape for QgsMapTool() is a Cross icon which I could use for the Marquee tool.)

3 Answers 3

5

For PyQGIS 2:

The mouse cursor can be controlled by calling the setCursor method of any QWidget:

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

cursor = QCursor()
cursor.setShape(Qt.WhatsThisCursor)
qgis.utils.iface.mainWindow().centralWidget().setCursor(cursor)

Or alternatively on the application level:

QApplication.instance().setOverrideCursor(cursor)
2

For PyQGIS 3, using a Python context manager:

It's recommended as it will make sure the cursor is set back in case of error when leaving the block of code.

from qgis.PyQt.QtCore import Qt
from qgis.utils import OverrideCursor

with OverrideCursor(Qt.WaitCursor):
    # Do something here ...
0

For PyQGIS 3:

from PyQt5.QtGui import QGuiApplication

QGuiApplication.setOverrideCursor(Qt.WaitCursor)

# do something here ...

QGuiApplication.restoreOverrideCursor()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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