Developing QGIS plugins with Python HOWTO
Architecture for Python plugins is still in development and things may change.
Developing plugins with Python requires QGIS 0.9.0 or newer version.
This tutorial requires QGIS 1.0.0 and higher
Introduction
With development of PythonBindings for QGIS it's now possible to create plugins in Python programming language. In comparison with classical plugins written in C++ these should be easier to write, understand and maintain due the nature of the language.
Note that you must compile QGIS with optional Python support or specific binding should be installed.
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.
This document assumes that you're already familiar with:
If not, please check the appropriate documentation.
Writing a plugin
Since the introduction of python plugins in QGIS, a number of plugins have appeared - on PluginRepositories page you can find some of them, you can use their source to learn more about programming with PyQGIS or find out whether you're not duplicating development effort.
Steps
- Idea
- create files
- write code
- test
- publish
Idea: Have an idea about what you want to do with your new QGIS plugin. Why do you do it? What problem do you want to solve? Is there already another plugin for that problem?
Create files: Create the files described nrxt. A starting point (init.py). A main python plugin body(plugin.py). A form in QT-Designer(form.ui), with it's resources.qrc.
Write code: Write the code inside the plugin.py
Test: Close and re-open QGIS and import your plugin again. Check if everything is OK.
Publish: Publish your plugin in QGIS repository or make your own repository as an "arsenal" of personal "GIS weapons"
Ideas
Ready to create a plugin but no idea what to do? PythonPluginIdeas page lists wishes from the community!
Creating necessary files
Here's the directory structure of our example plugin:
PYTHON_PLUGINS_PATH/
testplug/
__init__.py
plugin.py
resources.qrc
resources.py
form.ui
form.py
init.py = The starting point of the plugin. Contains general info, version, name and main class.
plugin.py = The main working code of the plugin. Contains all the information about the actions of the plugin and the main code.
resources.qrc = The .xml document created by QT-Designer. Contains relative paths to resources of the forms.
resources.py = The translation of the .qrc file described above to Python.
form.ui = The GUI created by QT-Designer.
form.py = The translation of the form.ui described above to Python.
Here and there are two automated ways of creating the basic files (skeleton) of a typical QGIS Python plugin. Useful to help you start with a typical plugin.
Writing code
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:
1 def name():
2 return "My testing plugin"
3 def description():
4 return "This plugin has no real use."
5 def version():
6 return "Version 0.1"
7 def qgisMinimumVersion():
8 return "1.0"
9 def authorName():
10 return "Developer"
11 def classFactory(iface):
12 # load TestPlugin class from file testplugin.py
13 from testplugin import TestPlugin
14 return TestPlugin(iface)
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):
1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 from qgis.core import *
4 # initialize Qt resources from file resouces.py
5 import resources
6 class TestPlugin:
7 def __init__(self, iface):
8 # save reference to the QGIS interface
9 self.iface = iface
10 def initGui(self):
11 # create action that will start plugin configuration
12 self.action = QAction(QIcon(":/plugins/testplug/icon.png"), "Test plugin", self.iface.mainWindow())
13 self.action.setWhatsThis("Configuration for test plugin")
14 self.action.setStatusTip("This is status tip")
15 QObject.connect(self.action, SIGNAL("triggered()"), self.run)
16 # add toolbar button and menu item
17 self.iface.addToolBarIcon(self.action)
18 self.iface.addPluginMenu("&Test plugins", self.action)
19 # connect to signal renderComplete which is emitted when canvas rendering is done
20 QObject.connect(self.iface.MapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
21 def unload(self):
22 # remove the plugin menu item and icon
23 self.iface.removePluginMenu("&Test plugins",self.action)
24 self.iface.removeToolBarIcon(self.action)
25 # disconnect form signal of the canvas
26 QObject.disconnect(self.iface.MapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
27
28 def run(self):
29 # create and show a configuration dialog or something similar
30 print "TestPlugin: run called!"
31 def renderTest(self, painter):
32 # use painter for drawing to map canvas
33 print "TestPlugin: renderTest called!"
Only functions of the plugin that must exist are initGui() and unload(). These functions are called when 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's good to use a prefix that won't collide with other plugins or any parts of QGIS, otherwise you might get resources you didn't 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.
Testing
Releasing the plugin
Once your plugin is ready and you think the plugin could be helpful for some people, don't 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'd 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 PluginRepositories.