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. |
Methods
Returns associated action with map tool or |
|
Returns associated button with map tool or |
|
returns pointer to the tool's map canvas |
|
Returns if the current map tool active on the map canvas |
|
Returns the map layer with the matching ID, or |
|
Use this to associate a QAction to this maptool. |
|
Use this to associate a button to this maptool. |
|
Sets the tool's name. |
|
Transforms a point from map coordinates to screen coordinates. |
|
Transforms a point from screen coordinates to layer coordinates. |
|
Transforms a point from map coordinates to layer coordinates. |
|
Transforms a point from screen coordinates to map coordinates. |
|
Transforms a point from layer coordinates to map coordinates (which is different in case reprojection is used). |
|
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.
called when set as currently active map tool |
|
Mouse double-click event for overriding. |
|
Mouse move event for overriding. |
|
Mouse press event for overriding. |
|
Mouse release event for overriding. |
|
Tooltip event for overriding. |
|
convenient method to clean members |
|
called when map tool is being deactivated |
|
Returns the flags for the map tool. |
|
gesture event for overriding. |
|
Key event for overriding. |
|
Key event for overriding. |
|
Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click. |
|
Allows the tool to populate and customize the given menu, prior to showing it in response to a right-mouse button click. |
|
Called when the map tool is being activated while it is already active. |
|
Sets a user defined cursor |
|
Mouse wheel event for overriding. |
Static Methods
Gets search radius in mm. |
|
Gets search radius in map units for given context. |
Signals
Emitted when the map tool is activated. |
|
Emitted when the map tool is deactivated. |
|
Emitted when the previous message from the tool should be cleared from the application message bar. |
|
Emitted when a message should be shown to the user in the application message bar. |
|
Emitted when the map tool is activated, while it is already active. |
Attributes
- 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]
- 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 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
None
if no layers could be found.This method searches both layers associated with the map canvas (see
QgsMapCanvas.layers()
) and layers from theQgsProject
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 aQgsProject
.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
- 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.See also
- 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 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.menu
will 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
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:
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]¶
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
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 tolayer
coordinates.- Parameters:
layer (Optional[QgsMapLayer])
point (QPoint)
- Return type:
- toLayerCoordinates(self, layer: QgsMapLayer | None, point: QgsPointXY) QgsPointXY [source]
Transforms a
point
from map coordinates tolayer
coordinates.- Parameters:
layer (Optional[QgsMapLayer])
point (
QgsPointXY
)
- Return type:
- toLayerCoordinates(self, layer: QgsMapLayer | None, rect: QgsRectangle) QgsRectangle [source]
Transforms a
rect
from map coordinates tolayer
coordinates.- Parameters:
layer (Optional[QgsMapLayer])
rect (
QgsRectangle
)
- Return type:
- toLayerCoordinatesV2(self, layer: QgsMapLayer | None, point: QgsPoint) QgsPoint ¶
Transforms a
point
from map coordinates tolayer
coordinates.- Parameters:
layer (Optional[QgsMapLayer])
point (QgsPoint)
- Return type:
- toMapCoordinates(self, point: QPoint) QgsPointXY [source]¶
Transforms a
point
from screen coordinates to map coordinates.- Parameters:
point (QPoint)
- Return type:
- toMapCoordinates(self, layer: QgsMapLayer | None, point: QgsPointXY) QgsPointXY [source]
Transforms a
point
fromlayer
coordinates to map coordinates (which is different in case reprojection is used).- Parameters:
layer (Optional[QgsMapLayer])
point (
QgsPointXY
)
- Return type:
- toMapCoordinatesV2(self, layer: QgsMapLayer | None, point: QgsPoint) QgsPoint ¶
Transforms a
point
fromlayer
coordinates to map coordinates (which is different in case reprojection is used).- Parameters:
layer (Optional[QgsMapLayer])
point (QgsPoint)
- Return type: