16.2. Code Snippets

Hint

The code snippets on this page need the following imports if you’re outside the pyqgis console:

 1from qgis.core import (
 2    QgsProject,
 3)
 4
 5from qgis.gui import (
 6    QgsOptionsWidgetFactory,
 7    QgsOptionsPageWidget
 8)
 9
10from qgis.PyQt.QtCore import Qt
11from qgis.PyQt.QtWidgets import QMessageBox, QAction, QHBoxLayout
12from qgis.PyQt.QtGui import QIcon

This section features code snippets to facilitate plugin development.

16.2.1. How to call a method by a key shortcut

In the plug-in add to the initGui()

self.key_action = QAction("Test Plugin", self.iface.mainWindow())
self.iface.registerMainWindowAction(self.key_action, "Ctrl+I")  # action triggered by Ctrl+I
self.iface.addPluginToMenu("&Test plugins", self.key_action)
self.key_action.triggered.connect(self.key_action_triggered)

To unload() add

self.iface.unregisterMainWindowAction(self.key_action)

The method that is called when CTRL+I is pressed

def key_action_triggered(self):
  QMessageBox.information(self.iface.mainWindow(),"Ok", "You pressed Ctrl+I")

16.2.2. Interface for plugin in the options dialog

You can add a custom plugin options tab to Settings ► Options. This is preferable over adding a specific main menu entry for your plugin’s options, as it keeps all of the QGIS application settings and plugin settings in a single place which is easy for users to discover and navigate.

The following snippet will just add a new blank tab for the plugin’s settings, ready for you to populate with all the options and settings specific to your plugin. You can split the following classes into different files. In this example, we are adding two classes into the main mainPlugin.py file.

 1class MyPluginOptionsFactory(QgsOptionsWidgetFactory):
 2
 3    def __init__(self):
 4        super().__init__()
 5
 6    def icon(self):
 7        return QIcon('icons/my_plugin_icon.svg')
 8
 9    def createWidget(self, parent):
10        return ConfigOptionsPage(parent)
11
12
13class ConfigOptionsPage(QgsOptionsPageWidget):
14
15    def __init__(self, parent):
16        super().__init__(parent)
17        layout = QHBoxLayout()
18        layout.setContentsMargins(0, 0, 0, 0)
19        self.setLayout(layout)

Finally we are adding the imports and modifying the __init__ function:

 1from qgis.PyQt.QtWidgets import QHBoxLayout
 2from qgis.gui import QgsOptionsWidgetFactory, QgsOptionsPageWidget
 3
 4
 5class MyPlugin:
 6    """QGIS Plugin Implementation."""
 7
 8    def __init__(self, iface):
 9        """Constructor.
10
11        :param iface: An interface instance that will be passed to this class
12            which provides the hook by which you can manipulate the QGIS
13            application at run time.
14        :type iface: QgsInterface
15        """
16        # Save reference to the QGIS interface
17        self.iface = iface
18
19
20    def initGui(self):
21        self.options_factory = MyPluginOptionsFactory()
22        self.options_factory.setTitle(self.tr('My Plugin'))
23        iface.registerOptionsWidgetFactory(self.options_factory)
24
25    def unload(self):
26        iface.unregisterOptionsWidgetFactory(self.options_factory)

Tip

Add custom tabs to a vector layer properties dialog

You can apply a similar logic to add the plugin custom option to the layer properties dialog using the classes QgsMapLayerConfigWidgetFactory and QgsMapLayerConfigWidget.