matt walsh thinkwhere_qgis_usergroup_pyqt

15
Blinging QGIS plugins (Introduction to PyQt) By Matthew Walsh

Upload: ross-mcdonald

Post on 20-Jul-2015

75 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

Blinging QGIS plugins (Introduction to PyQt)

By Matthew Walsh

Page 2: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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 QtDesigner, Check “C:\OSGeo4W\bin\designer.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…

Page 3: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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!

Page 4: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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???

Page 5: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

Why learn PyQt?

• Enhance the default plugin dialog created by the ‘plugin builder’ plugin.

• Do more with your plugin.

Page 6: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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

Page 7: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

Install the plugin

• Download and unzip the plugin.

• Copy into location where QGIS searches for plugins.

• Recommended: “C:\Users\matthew.walsh\.qgis2\python\plugins”

• Alternative: “C:\OSGeo4W\apps\qgis\python\plugins”

• Install ‘plugin reloader’ plugin.

Page 8: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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.

Page 9: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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.

Page 10: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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.

Page 11: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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’.

Page 12: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

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.

Page 13: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

Tableview Demo

• Uncomment code to populate the tableviewusing the model/view based approach.

• Uncomment code to enable incremental search/filter.

Page 15: Matt Walsh thinkWhere_QGIS_usergroup_pyqt

thinkWhereGlendevon House

Castle Business Park

Stirling

FK9 4TZ

t: 01786 476060 f: 01786 476099e: [email protected]