from qgis.PyQt.QtWidgets import QAction # Create a new action action = QAction("Save", self) # Set the status tip for the action action.setStatusTip("Save the current project") # Add the action to a toolbar or menu toolbar.addAction(action)
from qgis.PyQt.QtWidgets import QAction from qgis.core import QgsProject # Create a new action action = QAction("Zoom to layer", self) # Set the status tip for the action action.setStatusTip("Zoom to the selected layer") # Define a function to handle the action def zoom_to_layer(): layer = QgsProject.instance().mapLayer(123) iface.actionZoomToLayer().trigger() # Connect the action to the function action.triggered.connect(zoom_to_layer) # Add the action to a toolbar or menu toolbar.addAction(action)In this example, we create a new `QAction` widget with the label "Zoom to layer". We set the status tip for the action to "Zoom to the selected layer". We define a function `zoom_to_layer` that gets the currently selected layer and zooms the map canvas to that layer. We connect the action to the `zoom_to_layer` function using the `triggered` signal. Finally, we add the action to a toolbar. Package library: `qgis.PyQt.QtWidgets` and `qgis.core`