Slides to support Matt Walsh's workshop on customising a QGIS plugin interface using Qt Designer / Creator given at the 3rd QGIS UK user group meeting in Edinburgh on 5th May 2015
2. Installing Qt Creator/Designer
• Creator is the full Qt IDE, we only need designer
(GUI Designer tools).
• OSGeo4W install of QGIS ships with its own Qt
Designer, Check “C:OSGeo4Wbindesigner.exe”.
• Download Qt Creator: Qt Creator 3.4.0 for Windows
(71 MB).
• Ubuntu/Linux: along the lines of: “sudo apt-get
install qt4-designer”.
• .exe on USB Sticks…
3. What is Qt?
• Qt is a cross platform Application framework.
• Developed by Nokia for the phone market
• Open source
• QGIS is built with Qt in C++.
• All user interface aspects of QGIS use Qt.
• i.e. Buttons, Menus, Attribute tables, Plugins!
4. PyQt
• Python bindings to Qt
• Allows us to write Python plugins for QGIS.
• PyQt + PyQGIS = QGIS plugin
• QGIS 2.8 uses PyQt4
• Moving to PyQt5/Python3 in the near future???
5. Why learn PyQt?
• Enhance the default plugin dialog created by
the ‘plugin builder’ plugin.
• Do more with your plugin.
6. Workshop covers…
• Install a plugin
• Modify user interface plugin.
• Compile to ‘.py’ with ‘pyuic4’ command line tool
• Connect a button.
• Populate our own table with attributes from
layer.
• Search layer based on attribute values
7. Install the plugin
• Download and unzip the plugin.
• Copy into location where QGIS searches for
plugins.
• Recommended:
“C:Usersmatthew.walsh.qgis2pythonplugins”
• Alternative:
“C:OSGeo4Wappsqgispythonplugins”
• Install ‘plugin reloader’ plugin.
8. Compiling at runtime
1. Open the ‘.ui’ file with Qt Creator/Designer.
2. Make a change. i.e. Move buttons or change
text of ‘windowTitle’ property.
3. Save changes (but don’t close Qt Creator).
4. Reload the plugin in QGIS using the ‘plugin
reloader’.
5. See the change in QGIS.
9. Precompile using pyuic4
• Pyuic4 – Command line interface to UIC (the
module we used in our code to compile at
runtime).
• Located in the QGIS ‘bin’ folder.
1. Make another visible change to the ‘.ui’ file
2. Compile with pyuic4.
3. View changes in QGIS.
10. Runtime vs Pre-compiled
• Compiling at runtime makes for easy
development with changes reflected quickly.
• Slower than pre-compiled (.ui file is read on
QGIS startup). Could be noticeably slow for
large projects.
• Pre-compiled allows you to see and change
the code. Useful for complex
custom/behaviour.
11. Edit the plugin ui
• Add LineEdit (input widgets) and change
objectName to ‘searchLineEdit’.
• Add button, change objectName to
‘searchPushButton’ and change display text on
the button.
• Add Table View and change objectName to
‘attributeTableView’.
• Add a label and remove display text and
change objectName to ‘searchLabel’.
12. Layouts and Spacers
1. Add some horizontal spacers to control the
relative position of the button and line edit.
2. Ensure the tableview sizePolicy is
‘Expanding’.
3. Right click on dlg > ‘Lay out’ > ‘Lay out in a
grid’.
4. Drag items around and change spacers until
you are happy with the layout.
• Multiple layouts can be added to one form.
13. Tableview Demo
• Uncomment code to populate the tableview
using the model/view based approach.
• Uncomment code to enable incremental
search/filter.
Pre-requisites are QGIS, Text Editor/Python IDE (pycharm/Pyscripter)
Qt or ‘Cute’
Purchased by Nokia from Trolltec in 2009
Building blocks of the QGIS GUI
Currently Qt4 could soon be Qt5 which is Python 3, maybe at QGIS 3?
Demo default dialog in QGIS.
Do more with plugins, e.g. multiple dialogs, tables, etc
If in ‘users’ folder then all versions/instances of QGIS will pickup plugin.
Also different location for standalone install.
DEMO installing the plugin
Check everyone's plugin is working
Explain compiling
Explain/demo code
Demo – Look at compiled py file with definition of dialog.
Generally recommend runtime compiling as this is just more practical for development
Could compile to when going to production