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.

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]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

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]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

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]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

signal messageEmitted[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

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]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

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().

searchRadiusMU(canvas: Optional[QgsMapCanvas]) -> float Gets search radius in map units for given canvas. Used by identify, tip etc. The values is calculated from searchRadiusMM().

Parameters:

context (QgsRenderContext)

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.

toLayerCoordinates(self, layer: Optional[QgsMapLayer], point: QgsPointXY) -> QgsPointXY Transforms a point from map coordinates to layer coordinates.

toLayerCoordinates(self, layer: Optional[QgsMapLayer], rect: QgsRectangle) -> QgsRectangle Transforms a rect from map coordinates to layer coordinates.

Parameters:
Return type:

QgsPointXY

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.

toMapCoordinates(self, layer: Optional[QgsMapLayer], point: QgsPointXY) -> QgsPointXY Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used).

Parameters:

point (QPoint)

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])