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¶
Base classes¶
Subclasses¶
Base class for map tools that edit vector geometry. |
|
A map tool that simply emits a point when clicking on the map. |
|
A map tool that emits an extent from a rectangle drawn onto the map canvas. |
|
Map tool for identifying features in layers. |
|
A map tool for panning the map. |
|
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
Noneif no action is associated- Return type:
Optional[QAction]
- 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
Noneif 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
- 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:
- 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
Noneif no layers could be found.This method searches both layers associated with the map canvas (see
QgsMapCanvas.layers()) and layers from theQgsProjectassociated with the canvas. It can be used to resolve layer IDs to layers which may be visible in the canvas, but not associated with aQgsProject.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.menuwill be initially populated with a set of default, generic actions. Any new actions added to the menu should be correctly parented tomenu.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.menuwill be initially populated with a set of default, generic actions. Any new actions added to the menu should be correctly parented tomenu.A pointer to the map mouse
eventcan 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:
menu (Optional[QMenu])
event (Optional[QgsMapMouseEvent])
- 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
Added in version 3.20.
- Parameters:
name (Optional[str])
- toCanvasCoordinates(self, point: QgsPointXY) QPoint[source]¶
Transforms a
pointfrom map coordinates to screen coordinates.- Parameters:
point (QgsPointXY)
- Return type:
QPoint
- toLayerCoordinates(self, layer: QgsMapLayer | None, point: QPoint) QgsPointXY[source]¶
Transforms a
pointfrom screen coordinates tolayercoordinates.toLayerCoordinates(self, layer: Optional[QgsMapLayer], point:
QgsPointXY) -> QgsPointXY Transforms apointfrom map coordinates tolayercoordinates.toLayerCoordinates(self, layer: Optional[QgsMapLayer], rect:
QgsRectangle) -> QgsRectangle Transforms arectfrom map coordinates tolayercoordinates.- Parameters:
layer (Optional[QgsMapLayer])
point (QPoint)
- Return type:
- toLayerCoordinatesV2(self, layer: QgsMapLayer | None, point: QgsPoint) QgsPoint¶
Transforms a
pointfrom map coordinates tolayercoordinates.- Parameters:
layer (Optional[QgsMapLayer])
point (QgsPoint)
- Return type:
- toMapCoordinates(self, point: QPoint) QgsPointXY[source]¶
Transforms a
pointfrom screen coordinates to map coordinates.toMapCoordinates(self, layer: Optional[QgsMapLayer], point:
QgsPointXY) -> QgsPointXY Transforms apointfromlayercoordinates to map coordinates (which is different in case reprojection is used).- Parameters:
point (QPoint)
- Return type:
- toMapCoordinatesV2(self, layer: QgsMapLayer | None, point: QgsPoint) QgsPoint¶
Transforms a
pointfromlayercoordinates to map coordinates (which is different in case reprojection is used).- Parameters:
layer (Optional[QgsMapLayer])
point (QgsPoint)
- Return type: