Skip to content

Qgis button

Import
from pyqgis_wrapper.application import QgisZoomOut, QgisZoomIn, QgisPan, QgisIdentify

QgisActionButton(canvas, toolbar, name, checkable=True, parent=None)

Bases: abc.ABC

Base class for the canvas button

Parameters:

Name Type Description Default
canvas qgis.gui.QgsMapCanvas

Canvas to link the button to

required
toolbar qgis.PyQt.QtWidgets.QToolBar

Toolbar in which the button will appear

required
name str

Name of the button

required
checkable bool

If True the button will have an on/off state, defaults to True

True
parent typing.Optional[qgis.PyQt.QtWidgets.QWidget]

Parent to use for display a centedred message Fallback to the canavs if n parent is provided, defaults to None

None
Source code in pyqgis_wrapper/application/qgis_app.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def __init__(
    self,
    canvas: QgsMapCanvas,
    toolbar: QToolBar,
    name: str,
    checkable: bool = True,
    parent: Optional[QWidget] = None,
):
    """
    :param canvas: Canvas to link the button to
    :type canvas: QgsMapCanvas
    :param toolbar: Toolbar in which the button will appear
    :type toolbar: QToolBar
    :param name: Name of the button
    :type name: str
    :param checkable: If True the button
    will have an on/off state, defaults to True
    :type checkable: bool, optional
    :param parent: Parent to use for display a centedred message
    Fallback to the canavs if n parent is provided, defaults to None
    :type parent: Optional[QWidget], optional
    """
    self.canvas = canvas
    self.toolbar = toolbar
    self.create_action(name, checkable)
    self.create_tool()
    self.toolbar.addAction(self.action)
    self.parent = parent or canvas  # Fallback to canvas if no parent given

create_action(name, checkable)

Create an action button and connect it to an event.

Parameters:

Name Type Description Default
name str

Name of the button

required
checkable bool

If True the button will have an on/off state

required
Source code in pyqgis_wrapper/application/qgis_app.py
88
89
90
91
92
93
94
95
96
97
98
99
def create_action(self, name: str, checkable: bool):
    """
    Create an action button and connect it to an event.

    :param name: Name of the button
    :type name: str
    :param checkable: If True the button will have an on/off state
    :type checkable: bool
    """
    self.action = QAction(name, self.toolbar)
    self.action.setCheckable(checkable)
    self.action.triggered.connect(self.on_trigger)

create_tool() abstractmethod

Create a tool to be triggered when an event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
101
102
103
104
105
106
@abstractmethod
def create_tool(self):
    """
    Create a tool to be triggered when an event occurs.
    """
    pass

on_trigger() abstractmethod

Call to the tool when the event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
108
109
110
111
112
113
@abstractmethod
def on_trigger(self):
    """
    Call to the tool when the event occurs.
    """
    pass

QgisZoomOut(canvas, toolbar, name, checkable=True, parent=None)

Bases: pyqgis_wrapper.application.qgis_app.QgisActionButton

Zoom out button

Source code in pyqgis_wrapper/application/qgis_app.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def __init__(
    self,
    canvas: QgsMapCanvas,
    toolbar: QToolBar,
    name: str,
    checkable: bool = True,
    parent: Optional[QWidget] = None,
):
    """
    :param canvas: Canvas to link the button to
    :type canvas: QgsMapCanvas
    :param toolbar: Toolbar in which the button will appear
    :type toolbar: QToolBar
    :param name: Name of the button
    :type name: str
    :param checkable: If True the button
    will have an on/off state, defaults to True
    :type checkable: bool, optional
    :param parent: Parent to use for display a centedred message
    Fallback to the canavs if n parent is provided, defaults to None
    :type parent: Optional[QWidget], optional
    """
    self.canvas = canvas
    self.toolbar = toolbar
    self.create_action(name, checkable)
    self.create_tool()
    self.toolbar.addAction(self.action)
    self.parent = parent or canvas  # Fallback to canvas if no parent given

create_tool()

Create a tool to be triggered when an event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
139
140
141
142
143
def create_tool(self):
    """
    Create a tool to be triggered when an event occurs.
    """
    self.tool = QgsMapToolZoom(self.canvas, True)

on_trigger()

Call to the tool when the event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
145
146
147
148
149
def on_trigger(self):
    """
    Call to the tool when the event occurs.
    """
    self.canvas.setMapTool(self.tool)

QgisZoomIn(canvas, toolbar, name, checkable=True, parent=None)

Bases: pyqgis_wrapper.application.qgis_app.QgisActionButton

Zoom In button

Source code in pyqgis_wrapper/application/qgis_app.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def __init__(
    self,
    canvas: QgsMapCanvas,
    toolbar: QToolBar,
    name: str,
    checkable: bool = True,
    parent: Optional[QWidget] = None,
):
    """
    :param canvas: Canvas to link the button to
    :type canvas: QgsMapCanvas
    :param toolbar: Toolbar in which the button will appear
    :type toolbar: QToolBar
    :param name: Name of the button
    :type name: str
    :param checkable: If True the button
    will have an on/off state, defaults to True
    :type checkable: bool, optional
    :param parent: Parent to use for display a centedred message
    Fallback to the canavs if n parent is provided, defaults to None
    :type parent: Optional[QWidget], optional
    """
    self.canvas = canvas
    self.toolbar = toolbar
    self.create_action(name, checkable)
    self.create_tool()
    self.toolbar.addAction(self.action)
    self.parent = parent or canvas  # Fallback to canvas if no parent given

create_tool()

Create a tool to be triggered when an event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
121
122
123
124
125
def create_tool(self):
    """
    Create a tool to be triggered when an event occurs.
    """
    self.tool = QgsMapToolZoom(self.canvas, False)

on_trigger()

Call to the tool when the event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
127
128
129
130
131
def on_trigger(self):
    """
    Call to the tool when the event occurs.
    """
    self.canvas.setMapTool(self.tool)

QgisPan(canvas, toolbar, name, checkable=True, parent=None)

Bases: pyqgis_wrapper.application.qgis_app.QgisActionButton

Pan button

Source code in pyqgis_wrapper/application/qgis_app.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def __init__(
    self,
    canvas: QgsMapCanvas,
    toolbar: QToolBar,
    name: str,
    checkable: bool = True,
    parent: Optional[QWidget] = None,
):
    """
    :param canvas: Canvas to link the button to
    :type canvas: QgsMapCanvas
    :param toolbar: Toolbar in which the button will appear
    :type toolbar: QToolBar
    :param name: Name of the button
    :type name: str
    :param checkable: If True the button
    will have an on/off state, defaults to True
    :type checkable: bool, optional
    :param parent: Parent to use for display a centedred message
    Fallback to the canavs if n parent is provided, defaults to None
    :type parent: Optional[QWidget], optional
    """
    self.canvas = canvas
    self.toolbar = toolbar
    self.create_action(name, checkable)
    self.create_tool()
    self.toolbar.addAction(self.action)
    self.parent = parent or canvas  # Fallback to canvas if no parent given

create_tool()

Create a tool to be triggered when an event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
157
158
159
160
161
def create_tool(self):
    """
    Create a tool to be triggered when an event occurs.
    """
    self.tool = QgsMapToolPan(self.canvas)

on_trigger()

Call to the tool when the event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
163
164
165
166
167
def on_trigger(self):
    """
    Call to the tool when the event occurs.
    """
    self.canvas.setMapTool(self.tool)

QgisIdentify(canvas, toolbar, name, checkable=True, parent=None)

Bases: pyqgis_wrapper.application.qgis_app.QgisActionButton

Identify button

Source code in pyqgis_wrapper/application/qgis_app.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def __init__(
    self,
    canvas: QgsMapCanvas,
    toolbar: QToolBar,
    name: str,
    checkable: bool = True,
    parent: Optional[QWidget] = None,
):
    """
    :param canvas: Canvas to link the button to
    :type canvas: QgsMapCanvas
    :param toolbar: Toolbar in which the button will appear
    :type toolbar: QToolBar
    :param name: Name of the button
    :type name: str
    :param checkable: If True the button
    will have an on/off state, defaults to True
    :type checkable: bool, optional
    :param parent: Parent to use for display a centedred message
    Fallback to the canavs if n parent is provided, defaults to None
    :type parent: Optional[QWidget], optional
    """
    self.canvas = canvas
    self.toolbar = toolbar
    self.create_action(name, checkable)
    self.create_tool()
    self.toolbar.addAction(self.action)
    self.parent = parent or canvas  # Fallback to canvas if no parent given

create_tool()

Create a tool to be triggered when an event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
175
176
177
178
179
180
181
182
def create_tool(self):
    """
    Create a tool to be triggered when an event occurs.
    """
    self.tool = QgsMapToolIdentify(self.canvas)
    self.tool.setCursor(Qt.CrossCursor)
    self.point_tool = QgsMapToolEmitPoint(self.canvas)
    self.point_tool.canvasClicked.connect(self.on_click)

on_trigger()

Call to the tool when the event occurs.

Source code in pyqgis_wrapper/application/qgis_app.py
184
185
186
187
188
def on_trigger(self):
    """
    Call to the tool when the event occurs.
    """
    self.canvas.setMapTool(self.point_tool)

on_click(point, button)

Identify the active layer (vector or raster) at the clicked position.

Parameters:

Name Type Description Default
point qgis.core.QgsPointXY

Coordinates of the mouse event

required
button qgis.PyQt.QtCore.Qt.MouseButton

Button clicked (unused) but i have to use it for the events to work.

required
Source code in pyqgis_wrapper/application/qgis_app.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def on_click(self, point: QgsPointXY, button: Qt.MouseButton):
    """
    Identify the active layer (vector or raster) at the clicked position.

    :param point: Coordinates of the mouse event
    :type point: QgsPointXY
    :param button: Button clicked (unused) but i have to use it for the events to work.
    :type button: Qt.MouseButton
    """
    layer = self.canvas.currentLayer()
    if layer is None:
        QMessageBox.warning(self.parent, "No Layer", "Please select a layer.")
        return

    if layer.type() == QgsMapLayer.VectorLayer:
        self._identify_vector(point)
    elif layer.type() == QgsMapLayer.RasterLayer:
        self._identify_raster(point)
    else:
        QMessageBox.warning(
            self.parent, "Unsupported Layer", "Layer type not supported."
        )