Class: QgsAbstractMapToolHandler¶
An abstract base class for map tool handlers, implementing application level UI logic.
QgsAbstractMapToolHandler
subclasses automatically handle
all the necessary logic for toggling the map tool and enabling/disabling
the associated action when the QGIS application is in a state
permissible for the tool.
Creating these handlers avoids a lot of complex setup code and manual connections which are otherwise necessary to ensure that a map tool is correctly activated and deactivated when the state of the QGIS application changes (e.g. when the active layer is changed, when edit modes are toggled, when other map tools are switched to, etc).
### Example
class MyMapTool(QgsMapTool):
...
class MyMapToolHandler(QgsAbstractMapToolHandler):
def __init__(self, tool, action):
super().__init__(tool, action)
def isCompatibleWithLayer(self, layer, context):
# this tool can only be activated when an editable vector layer is selected
return isinstance(layer, QgsVectorLayer) and layer.isEditable()
my_tool = MyMapTool()
my_action = QAction('My Map Tool')
my_handler = MyMapToolHandler(my_tool, my_action)
iface.registerMapToolHandler(my_handler)
Added in version 3.16.
Note
This is an abstract class, with methods which must be implemented by a subclass.
The following methods must be implemented: isCompatibleWithLayer()
Abstract Methods
Returns |
Methods
Returns the action associated with toggling the tool. |
|
Returns the tool associated with this handler. |
Virtual Methods
In PyQGIS, only methods marked as virtual
can be safely overridden in a Python subclass of QgsAbstractMapToolHandler. See the FAQ for more details.
Sets the layer to use for the tool. |
- class qgis.gui.QgsAbstractMapToolHandler[source]¶
Bases:
object
- __init__(tool: QgsMapTool | None, action: QAction | None)
Constructor for a map tool handler for the specified
tool
.The
action
argument must be set to the action associated with switching to the tool.The ownership of neither
tool
noraction
is transferred, and the caller is responsible for ensuring that these objects exist for the lifetime of the handler.Warning
The handler will be responsible for creating the appropriate connections between the
action
and thetool
. These should NOT be manually connected elsewhere!- Parameters:
tool (Optional[QgsMapTool])
action (Optional[QAction])
- __init__(a0: QgsAbstractMapToolHandler)
- Parameters:
- class Context¶
Bases:
object
Context of a
QgsAbstractMapToolHandler
call.Added in version 3.16.
- dummy: bool¶
Placeholder only
- action(self) QAction | None [source]¶
Returns the action associated with toggling the tool.
- Return type:
Optional[QAction]
- abstract isCompatibleWithLayer(self, layer: QgsMapLayer | None, context: QgsAbstractMapToolHandler.Context) bool [source]¶
Returns
True
if the associated map tool is compatible with the specifiedlayer
.Additional information is available through the
context
argument.- Parameters:
layer (Optional[QgsMapLayer])
context (QgsAbstractMapToolHandler.Context)
- Return type:
bool
- mapTool(self) QgsMapTool | None [source]¶
Returns the tool associated with this handler.
- Return type:
Optional[QgsMapTool]
- virtual setLayerForTool(self, layer: QgsMapLayer | None)[source]¶
Sets the
layer
to use for the tool.Called whenever a new layer should be associated with the tool, e.g. as a result of the user selecting a different active layer.
The default implementation does nothing.
- Parameters:
layer (Optional[QgsMapLayer])