It is possible to create plugins in Python programming language. In comparison with classical plugins written in C++ these should be easier to write, understand, maintain and distribute due the dynamic nature of the Python language.
Python plugins are listed together with C++ plugins in QGIS plugin manager. They’re being searched for in these paths:
- UNIX/Mac: ~/.qgis/python/plugins and (qgis_prefix)/share/qgis/python/plugins
- Windows: ~/.qgis/python/plugins and (qgis_prefix)/python/plugins
Home directory (denoted by above ~) on Windows is usually something like C:\Documents and Settings\(user). Subdirectories of these paths are considered as Python packages that can be imported to QGIS as plugins.
Since the introduction of python plugins in QGIS, a number of plugins have appeared - on Plugin Repositories wiki page you can find some of them, you can use their source to learn more about programming with PyQGIS or find out whether you are not duplicating development effort. Ready to create a plugin but no idea what to do? Python Plugin Ideas wiki pagelists wishes from the community!
Here’s the directory structure of our example plugin:
PYTHON_PLUGINS_PATH/ testplug/ __init__.py plugin.py metadata.txt resources.qrc resources.py form.ui form.py
What is the meaning of the files:
Here and there are two automated ways of creating the basic files (skeleton) of a typical QGIS Python plugin. Also there is a QGIS plugin called Plugin Builder that creates plugin template from QGIS and don’t require internet connection. Useful to help you start with a typical plugin.
First, plugin manager needs to retrieve some basic information about the plugin such as its name, description etc. File __init__.py is the right place where to put this information:
def name(): return "My testing plugin" def description(): return "This plugin has no real use." def version(): return "Version 0.1" def qgisMinimumVersion(): return "1.0" def authorName(): return "Developer" def classFactory(iface): # load TestPlugin class from file testplugin.py from testplugin import TestPlugin return TestPlugin(iface)
In QGIS 1.9.90 plugins can be placed not only into Plugins menu but also into Raster, Vector, Database and Web menus. Therefore a new “category” metadata entry has been introduced. This metadata entry is used as tip for users and tells them where (in which menu) the plugin can be found. Allowed values for “category” are: Vector, Raster, Database, Web and Layers. For example, if your plugin will be available from Raster menu, add this to __init__.py:
def category(): return "Raster"
For QGIS >= 1.8 you need to add a metadata.txt (see here) An exampe for this metadata.txt:
; the next section is mandatory [general] name=HelloWorld qgisMinimumVersion=1.8 description=This is a plugin for greeting the (going multiline) world category=Raster version=version 1.2 ; end of mandatory metadata ; start of optional metadata changelog=this is a very very very very very very long multiline changelog ; tags are in comma separated value format, spaces are allowed tags=wkt,raster,hello world ; these metadata can be empty ; in a future version of the web application it will ; be probably possible to create a project on redmine ; if they are not filled homepage=http://www.itopen.it tracker=http://bugs.itopen.it repository=http://www.itopen.it/repo icon=icon.png ; experimental flag experimental=True ; deprecated flag (applies to the whole plugin and not only to the uploaded version) deprecated=False
One thing worth mentioning is classFactory() function which is called when the plugin gets loaded to QGIS. It receives reference to instance of QgisInterface and must return instance of your plugin - in our case it’s called TestPlugin. This is how should this class look like (e.g. testplugin.py):
from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * # initialize Qt resources from file resouces.py import resources class TestPlugin: def __init__(self, iface): # save reference to the QGIS interface self.iface = iface def initGui(self): # create action that will start plugin configuration self.action = QAction(QIcon(":/plugins/testplug/icon.png"), "Test plugin", self.iface.mainWindow()) self.action.setWhatsThis("Configuration for test plugin") self.action.setStatusTip("This is status tip") QObject.connect(self.action, SIGNAL("triggered()"), self.run) # add toolbar button and menu item self.iface.addToolBarIcon(self.action) self.iface.addPluginToMenu("&Test plugins", self.action) # connect to signal renderComplete which is emitted when canvas rendering is done QObject.connect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest) def unload(self): # remove the plugin menu item and icon self.iface.removePluginMenu("&Test plugins",self.action) self.iface.removeToolBarIcon(self.action) # disconnect form signal of the canvas QObject.disconnect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest) def run(self): # create and show a configuration dialog or something similar print "TestPlugin: run called!" def renderTest(self, painter): # use painter for drawing to map canvas print "TestPlugin: renderTest called!"
If you use QGIS 1.9.90 or higher and want to place your plugin into one of the new menus (Raster, Vector, Database or Web), you should modify the code of the initGui() and unload() functions. Since these new menus are available only in QGIS 1.9.90, our first step is to check if the running QGIS version has all necessary functions. If the new menus are available, we will place our plugin under this menu, otherwise we will use the old Plugins menu. Here is an example for Raster menu:
def initGui(self): # create action that will start plugin configuration self.action = QAction(QIcon(":/plugins/testplug/icon.png"), "Test plugin", self.iface.mainWindow()) self.action.setWhatsThis("Configuration for test plugin") self.action.setStatusTip("This is status tip") QObject.connect(self.action, SIGNAL("triggered()"), self.run) # check if Raster menu available if hasattr(self.iface, "addPluginToRasterMenu"): # Raster menu and toolbar available self.iface.addRasterToolBarIcon(self.action) self.iface.addPluginToRasterMenu("&Test plugins", self.action) else: # there is no Raster menu, place plugin under Plugins menu as usual self.iface.addToolBarIcon(self.action) self.iface.addPluginToMenu("&Test plugins", self.action) # connect to signal renderComplete which is emitted when canvas rendering is done QObject.connect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest) def unload(self): # check if Raster menu available and remove our buttons from appropriate # menu and toolbar if hasattr(self.iface, "addPluginToRasterMenu"): self.iface.removePluginRasterMenu("&Test plugins",self.action) self.iface.removeRasterToolBarIcon(self.action) else: self.iface.removePluginMenu("&Test plugins",self.action) self.iface.removeToolBarIcon(self.action) # disconnect form signal of the canvas QObject.disconnect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
A full list of methods that can be used to place plugin under these menus/toolbars is available in the API docs.
The only plugin functions that must exist are initGui() and unload(). These functions are called when the plugin is loaded and unloaded.
You can see that in initGui() we’ve used an icon from the resource file (called resources.qrc in our case):
<RCC> <qresource prefix="/plugins/testplug" > <file>icon.png</file> </qresource> </RCC>
It is good to use a prefix that will not collide with other plugins or any parts of QGIS, otherwise you might get resources you did not want. Now you just need to generate a Python file that will contain the resources. It’s done with pyrcc4 command:
pyrcc4 -o resources.py resources.qrc
And that’s all... nothing complicated :) If you’ve done everything correctly you should be able to find and load your plugin in plugin manager and see a message in console when toolbar icon or appopriate menu item is selected.
When working on a real plugin it’s wise to write the plugin in another (working) directory and create a makefile which will generate UI + resource files and install the plugin to your QGIS installation.
This documentation method requires Qgis version 1.5.
The documentation for the plugin can be written as HTML help files. The qgis.utils module provides a function, showPluginHelp() which will open the help file users browser, in the same way as other QGIS help.
The showPluginHelp`() function looks for help files in the same directory as the calling module. It will look for, in turn, index-ll_cc.html, index-ll.html, index-en.html, index-en_us.html and index.html, displaying whichever it finds first. Here ll_cc is the QGIS locale. This allows multiple translations of the documentation to be included with the plugin.
The showPluginHelp() function can also take parameters packageName, which identifies a specific plugin for which the help will be displayed, filename, which can replace “index” in the names of files being searched, and section, which is the name of an html anchor tag in the document on which the browser will be positioned.
This section features code snippets to facilitate plugin development.
In the plug-in add to the initGui():
self.keyAction = QAction("Test Plugin", self.iface.mainWindow()) self.iface.registerMainWindowAction(self.keyAction, "F7") # action1 is triggered by the F7 key self.iface.addPluginToMenu("&Test plugins", self.keyAction) QObject.connect(self.keyAction, SIGNAL("triggered()"),self.keyActionF7)
To unload() add:
The method that is called when F7 is pressed:
def keyActionF7(self): QMessageBox.information(self.iface.mainWindow(),"Ok", "You pressed F7")
Note: from QGIS 1.5 there is QgsLegendInterface class that allows some manipulation with list of layers within legend.
As there is currently no method to directly access the layers in the legend, here is a workaround how to toggle the layers using layer transparency:
def toggleLayer(self, lyrNr): lyr = self.iface.mapCanvas().layer(lyrNr) if lyr: cTran = lyr.getTransparency() lyr.setTransparency(0 if cTran > 100 else 255) self.iface.mapCanvas().refresh()
The method requires the layer number (0 being the top most) and can be called by:
def changeValue(self, value): layer = self.iface.activeLayer() if(layer): nF = layer.selectedFeatureCount() if (nF > 0): layer.startEditing() ob = layer.selectedFeaturesIds() b = QVariant(value) if (nF > 1): for i in ob: layer.changeAttributeValue(int(i),1,b) # 1 being the second column else: layer.changeAttributeValue(int(ob),1,b) # 1 being the second column layer.commitChanges() else: QMessageBox.critical(self.iface.mainWindow(),"Error", "Please select at least one feature from current layer") else: QMessageBox.critical(self.iface.mainWindow(),"Error","Please select a layer")
The method requires the one parameter (the new value for the attribute field of the selected feature(s)) and can be called by:
First add this code in the spot where you would like to debug:
# Use pdb for debugging import pdb # These lines allow you to set a breakpoint in the app pyqtRemoveInputHook() pdb.set_trace()
Then run QGIS from the command line.
On Linux do:
On Mac OS X do:
And when the application hits your breakpoint you can type in the console!
Once your plugin is ready and you think the plugin could be helpful for some people, do not hesitate to upload it to PyQGIS plugin repository. On that page you can find also packaging guidelines how to prepare the plugin to work well with the plugin installer. Or in case you would like to set up your own plugin repository, create a simple XML file that will list the plugins and their metadata, for examples see other plugin repositories.
On Linux there is no additional configuration needed to develop plug-ins. But on Windows you need to make sure you that you have the same environment settings and use the same libraries and interpreter as QGIS. The fastest way to do this, is to modify the startup batch file of QGIS.
If you used the OSGeo4W Installer, you can find this under the bin folder of your OSGoeW install. Look for something like C:\OSGeo4W\bin\qgis-unstable.bat.
I will illustrate how to set up the Pyscripter IDE. Other IDEs might require a slightly different approach:
Make a copy of qgis-unstable.bat and rename it pyscripter.bat.
Open it in an editor. And remove the last line, the one that starts qgis.
Add a line that points to the your pyscripter executable and add the commandline argument that sets the version of python to be used, in version 1.3 of qgis this is python 2.5.
Also add the argument that points to the folder where pyscripter can find the python dll used by qgis, you can find this under the bin folder of your OSGeoW install:
@echo off SET OSGEO4W_ROOT=C:\OSGeo4W call "%OSGEO4W_ROOT%"\bin\o4w_env.bat call "%OSGEO4W_ROOT%"\bin\gdal16.bat @echo off path %PATH%;%GISBASE%\bin Start C:\pyscripter\pyscripter.exe --python25 --pythondllpath=C:\OSGeo4W\bin
Now when you double click this batch file and it will start pyscripter.