How to write QGIS Python plugins

One of the advantages of QGIS is how well it integrates Python. There’s a Python console, but more importantly it is possible to expand QGIS with Python plugins. These come in two flavours:

  • GUI plugins have a graphical user interface (built with the Qt library) that can be customized
  • Processing plugins only allow for the selection of input and output data, but can be called by other code

A good explanation of how to get started is given on qgistutorials.com. There are two plugins that are essential: The Plugin Builder generates all code that is required, and the Plugin Reloader lets you reload your own plugin after you have made changes – otherwise it would be necessary to restart QGIS after each change.

Under Windows, with a current OSGeo4W installation of QGIS (3.28.2 at the time of writing) I noticed one difference with regard to the article mentioned above. In the compile.bat batch file, I only needed the first of the three call statements for setting the environment. I would recommend to add a line that copies the entire plugin from the location where you write it (I have dedicated folders for all programming, and these are added to a Git repository) to where QGIS expects it. Here’s an example:

@echo off
call "C:\OSGeo4W\bin\o4w_env.bat"
@echo on
pyrcc5 -o resources.py resources.qrc
cd ..
mkdir C:\Users\username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\my_plugin
copy my_plugin C:\Users\username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\my_plugin

If you run this after each change that you want to test, it will not only recompile the resources, but also copy the plugin to the proper location. You can then reload the plugin with the Plugin Reloader and test the changes. Program output is written to the Python Console  (which can be accessed from the Plugins menu), Python error messages are written to the Python error log.

qgistutorials.com also has a simple example about what you can do with Python in QGIS. Here are a couple of other information sources to help you find your way:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert