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.
Introduction
With development of PythonBindings for QGIS there's also implemented possibility to create plugins in Python programming language. In comparison with classical plugins made in C++ these should be easier to write, understand and maintain due the nature of the language.
Writing Python plugins is possible currently only with SVN trunk. Note that you must compile QGIS with optional Python support.
Python plugins are shown 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.
Existing and wanted plugins
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.
Ready to create a plugin but no idea what to do? PythonPluginIdeas page lists wishes from the community!
Writing a plugin
Here's a 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 clas.
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 type of, document created by QT-Designer. Contains relative paths to resources of the forms.
resources.py = The translation to python language, of the .qrc file described just above.
form.ui =The form created by QT-Designer
form.py =The translation to python language of the form.ui described just above
Steps
- Idea
- create files
- write code
- test
- publish
Idea: Have an idea about what you want to do with your new QGIS plugin. Why you do it? What problem do you want to solve? Is there already an other plugin for that?
Create files: Create the files described above. 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 inport again your plugin. Chack if everything is OK
Publish: Publish your plugin in QGIS repository or make your own repository as an "arsenal" of personal "GIS weapons"
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 classFactory(iface):
8 # load TestPlugin class from file testplugin.py
9 from testplugin import TestPlugin
10 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.xpm"), "Test plugin", self.iface.getMainWindow())
13 self.action.setWhatsThis("Configuration for test plugin")
14 QObject.connect(self.action, SIGNAL("activated()"), self.run)
15 # add toolbar button and menu item
16 self.iface.addToolBarIcon(self.action)
17 self.iface.addPluginMenu("&Test plugins", self.action)
18 # connect to signal renderComplete which is emitted when canvas rendering is done
19 QObject.connect(self.iface.getMapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
20 def unload(self):
21 # remove the plugin menu item and icon
22 self.iface.removePluginMenu("&Test plugins",self.action)
23 self.iface.removeToolBarIcon(self.action)
24 # disconnect form signal of the canvas
25 QObject.disconnect(self.iface.getMapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
26
27 def run(self):
28 # create and show a configuration dialog or something similar
29 print "TestPlugin: run called!"
30 def renderTest(self, painter):
31 # use painter for drawing to map canvas
32 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.xpm</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.
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.