Class: QgsMapTool

Abstract base class for all map tools.

Map tools are user interactive tools for manipulating the map canvas. For example map pan and zoom features are implemented as map tools.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsMapTool

Base classes

QObject

Subclasses

QgsMapToolEdit

Base class for map tools that edit vector geometry.

QgsMapToolEmitPoint

A map tool that simply emits a point when clicking on the map.

QgsMapToolExtent

A map tool that emits an extent from a rectangle drawn onto the map canvas.

QgsMapToolIdentify

Map tool for identifying features in layers.

QgsMapToolPan

A map tool for panning the map.

QgsMapToolZoom

A map tool for zooming into the map.

Methods

action

Returns associated action with map tool or None if no action is associated

button

Returns associated button with map tool or None if no button is associated

canvas

returns pointer to the tool's map canvas

isActive

Returns if the current map tool active on the map canvas

layer

Returns the map layer with the matching ID, or None if no layers could be found.

setAction

Use this to associate a QAction to this maptool.

setButton

Use this to associate a button to this maptool.

setToolName

Sets the tool's name.

toCanvasCoordinates

Transforms a point from map coordinates to screen coordinates.

toLayerCoordinates

Transforms a point from screen coordinates to layer coordinates.

toLayerCoordinatesV2

Transforms a point from map coordinates to layer coordinates.

toMapCoordinates

Transforms a point from screen coordinates to map coordinates.

toMapCoordinatesV2

Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used).

toolName

Emit map tool changed with the old tool

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsMapTool. See the FAQ for more details.

activate

called when set as currently active map tool

canvasDoubleClickEvent

Mouse double-click event for overriding.

canvasMoveEvent

Mouse move event for overriding.

canvasPressEvent

Mouse press event for overriding.

canvasReleaseEvent

Mouse release event for overriding.

canvasToolTipEvent

Tooltip event for overriding.

clean

convenient method to clean members

deactivate

called when map tool is being deactivated

flags

Returns the flags for the map tool.

gestureEvent

gesture event for overriding.

keyPressEvent

Key event for overriding.

keyReleaseEvent

Key event for overriding.

populateContextMenu

Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click.

populateContextMenuWithEvent

Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click.

reactivate

Called when the map tool is being activated while it is already active.

setCursor

Sets a user defined cursor

wheelEvent

Mouse wheel event for overriding.

Static Methods

searchRadiusMM

Gets search radius in mm.

searchRadiusMU

Gets search radius in map units for given context.

Signals

activated

Emitted when the map tool is activated.

deactivated

Emitted when the map tool is deactivated.

messageDiscarded

Emitted when the previous message from the tool should be cleared from the application message bar.

messageEmitted

Emitted when a message should be shown to the user in the application message bar.

reactivated

Emitted when the map tool is activated, while it is already active.

Attributes

AllowZoomRect

EditTool

ShowContextMenu

Transient

class qgis.gui.QgsMapTool[source]

Bases: QObject

__init__(canvas: QgsMapCanvas | None)

Constructor takes a map canvas as a parameter.

Parameters:

canvas (Optional[QgsMapCanvas])

AllowZoomRect = 8
EditTool = 4
class Flag

Bases: int

class Flags
class Flags(f: QgsMapTool.Flags | QgsMapTool.Flag)
class Flags(a0: QgsMapTool.Flags)

Bases: object

ShowContextMenu = 16
Transient = 2
action(self) QAction | None[source]

Returns associated action with map tool or None if no action is associated

Return type:

Optional[QAction]

virtual activate(self)[source]

called when set as currently active map tool

signal activated[source]

Emitted when the map tool is activated.

See also

deactivated()

button(self) QAbstractButton | None[source]

Returns associated button with map tool or None if no button is associated

Return type:

Optional[QAbstractButton]

canvas(self) QgsMapCanvas | None[source]

returns pointer to the tool’s map canvas

Return type:

Optional[QgsMapCanvas]

virtual canvasDoubleClickEvent(self, e: QgsMapMouseEvent | None)[source]

Mouse double-click event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QgsMapMouseEvent])

virtual canvasMoveEvent(self, e: QgsMapMouseEvent | None)[source]

Mouse move event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QgsMapMouseEvent])

virtual canvasPressEvent(self, e: QgsMapMouseEvent | None)[source]

Mouse press event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QgsMapMouseEvent])

virtual canvasReleaseEvent(self, e: QgsMapMouseEvent | None)[source]

Mouse release event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QgsMapMouseEvent])

virtual canvasToolTipEvent(self, e: QHelpEvent | None) bool[source]

Tooltip event for overriding. Default implementation does nothing. Returns whether the event was handled by the tool and should not be propagated further.

Added in version 3.22.

Parameters:

e (Optional[QHelpEvent])

Return type:

bool

virtual clean(self)[source]

convenient method to clean members

virtual deactivate(self)[source]

called when map tool is being deactivated

signal deactivated[source]

Emitted when the map tool is deactivated.

See also

activated()

virtual flags(self) QgsMapTool.Flags[source]

Returns the flags for the map tool.

Return type:

QgsMapTool.Flags

virtual gestureEvent(self, e: QGestureEvent | None) bool[source]

gesture event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QGestureEvent])

Return type:

bool

isActive(self) bool[source]

Returns if the current map tool active on the map canvas

Added in version 3.4.

Return type:

bool

virtual keyPressEvent(self, e: QKeyEvent | None)[source]

Key event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QKeyEvent])

virtual keyReleaseEvent(self, e: QKeyEvent | None)[source]

Key event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QKeyEvent])

layer(self, id: str | None) QgsMapLayer | None[source]

Returns the map layer with the matching ID, or None if no layers could be found.

This method searches both layers associated with the map canvas (see QgsMapCanvas.layers()) and layers from the QgsProject associated with the canvas. It can be used to resolve layer IDs to layers which may be visible in the canvas, but not associated with a QgsProject.

Added in version 3.22.

Parameters:

id (Optional[str])

Return type:

Optional[QgsMapLayer]

signal messageDiscarded[source]

Emitted when the previous message from the tool should be cleared from the application message bar.

See also

messageEmitted()

signal messageEmitted(message: str, level: Qgis.MessageLevel = Qgis.MessageLevel.Info)[source]

Emitted when a message should be shown to the user in the application message bar.

Parameters:
  • message (str)

  • level (Qgis.MessageLevel = Qgis.MessageLevel.Info)

virtual populateContextMenu(self, menu: QMenu | None)[source]

Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click.

menu will be initially populated with a set of default, generic actions. Any new actions added to the menu should be correctly parented to menu.

The default implementation does nothing.

Note

The context menu is only shown when the ShowContextMenu flag is present in flags().

Added in version 3.14.

Parameters:

menu (Optional[QMenu])

virtual populateContextMenuWithEvent(self, menu: QMenu | None, event: QgsMapMouseEvent | None) bool[source]

Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click.

menu will be initially populated with a set of default, generic actions. Any new actions added to the menu should be correctly parented to menu.

A pointer to the map mouse event can be provided to allow particular behavior depending on the map tool.

This method can return true to inform the caller that the menu was effectively populated.

The default implementation does nothing and returns false.

Note

The context menu is only shown when the ShowContextMenu flag is present in flags().

Added in version 3.18.

Parameters:
Return type:

bool

virtual reactivate(self)[source]

Called when the map tool is being activated while it is already active.

The default implementation emits the reactivated () signal.

Added in version 3.32.

signal reactivated[source]

Emitted when the map tool is activated, while it is already active.

Added in version 3.32.

static searchRadiusMM() float[source]

Gets search radius in mm. Used by identify, tip etc. The values is currently set in identify tool options (move somewhere else?) and defaults to Qgis.DEFAULT_SEARCH_RADIUS_MM.

Return type:

float

static searchRadiusMU(context: QgsRenderContext) float[source]

Gets search radius in map units for given context. Used by identify, tip etc. The values is calculated from searchRadiusMM().

Parameters:

context (QgsRenderContext)

Return type:

float

static searchRadiusMU(canvas: QgsMapCanvas | None) float[source]

Gets search radius in map units for given canvas. Used by identify, tip etc. The values is calculated from searchRadiusMM().

Parameters:

canvas (Optional[QgsMapCanvas])

Return type:

float

setAction(self, action: QAction | None)[source]

Use this to associate a QAction to this maptool. Then when the setMapTool method of mapcanvas is called the action state will be set to on. Usually this will cause e.g. a toolbutton to appear pressed in and the previously used toolbutton to pop out.

Parameters:

action (Optional[QAction])

setButton(self, button: QAbstractButton | None)[source]

Use this to associate a button to this maptool. It has the same meaning as setAction() function except it works with a button instead of an QAction.

Parameters:

button (Optional[QAbstractButton])

virtual setCursor(self, cursor: QCursor | Qt.CursorShape)[source]

Sets a user defined cursor

Parameters:

cursor (Union[QCursor, Qt.CursorShape])

setToolName(self, name: str | None)[source]

Sets the tool’s name.

See also

toolName()

Added in version 3.20.

Parameters:

name (Optional[str])

toCanvasCoordinates(self, point: QgsPointXY) QPoint[source]

Transforms a point from map coordinates to screen coordinates.

Parameters:

point (QgsPointXY)

Return type:

QPoint

toLayerCoordinates(self, layer: QgsMapLayer | None, point: QPoint) QgsPointXY[source]

Transforms a point from screen coordinates to layer coordinates.

Parameters:
Return type:

QgsPointXY

toLayerCoordinates(self, layer: QgsMapLayer | None, point: QgsPointXY) QgsPointXY[source]

Transforms a point from map coordinates to layer coordinates.

Parameters:
Return type:

QgsPointXY

toLayerCoordinates(self, layer: QgsMapLayer | None, rect: QgsRectangle) QgsRectangle[source]

Transforms a rect from map coordinates to layer coordinates.

Parameters:
Return type:

QgsRectangle

toLayerCoordinatesV2(self, layer: QgsMapLayer | None, point: QgsPoint) QgsPoint

Transforms a point from map coordinates to layer coordinates.

Parameters:
Return type:

QgsPoint

toMapCoordinates(self, point: QPoint) QgsPointXY[source]

Transforms a point from screen coordinates to map coordinates.

Parameters:

point (QPoint)

Return type:

QgsPointXY

toMapCoordinates(self, layer: QgsMapLayer | None, point: QgsPointXY) QgsPointXY[source]

Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used).

Parameters:
Return type:

QgsPointXY

toMapCoordinatesV2(self, layer: QgsMapLayer | None, point: QgsPoint) QgsPoint

Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used).

Parameters:
Return type:

QgsPoint

toolName(self) str[source]

Emit map tool changed with the old tool

See also

setToolName()

Return type:

str

virtual wheelEvent(self, e: QWheelEvent | None)[source]

Mouse wheel event for overriding. Default implementation does nothing.

Parameters:

e (Optional[QWheelEvent])