We use Qt Desinger to design QGIS plugin.
Development Tools
Install Plugin Builder (for creating Plugin project), Plugin Reloader (to refresh changes to the Plugin while developing) to assist with plugin development.
Use Qt Designer to help create Plugin interface.
When develope QGIS plugin, your source file folder and QGIS folder might be different. Instead of constantly copying files across, we can use Junction to creat symbolic link. Refer to the Readme.txt file under the plugin folder for the source/target locations.
junction <targetfolder>\<symbolicName> <Sourcefolder>
#for example:
#junction C:\Users\qliu\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\mxmds C:\pluginFolder\plugin
After the symbolic link is created for the first time, you need to restart QGIS if it’s already open to be able to see the plugin.
The plugin name searchable in QGIS plugin manager is the name under general section in metadata.txt. Other plugin information setting can also be found/changed in this file.
Compile
When need to compile the plugin, for example, after update the logo, run following using OSGeo4W Shell. Make sure the Python environment path variables are set.
pyrcc5 resources.qrc -o resource.py
Change icon
If you changed the icon.png, you need to run following comand to update the resources.qrc. Plugin icon was converted to a byte array and saved in resources.py. So if you want to change this icon you have to compile your resources.qrc again.
pyrcc5 -o resources.py resources.qrc
pyrcc5 resources.qrc -o resource.py
Packaging
When packaging the plugin, the mandatory files in the compressed (.zip) package are: metadata.txt and _init_.py. It would be nice to have a README and of course an icon to represent the plugin (resources.qrc). Following is an example of how a plugin.zip should look like.
plugin.zip
pluginfolder/
|-- i18n
| |-- translation_file_de.ts
|-- img
| |-- icon.png
| `-- iconsource.svg
|-- __init__.py
|-- Makefile
|-- metadata.txt
|-- more_code.py
|-- main_code.py
|-- README
|-- resources.qrc
|-- resources_rc.py
|-- ui_Qt_user_interface_file.ui
Other
To auto install required modules for the plugin, following code (for install matplotlib) might give some idea (not sure if it’s a good idea), assumed QGIS with OSGEO4W has been installed already.
import os
import sys
plugin_dir = os.path.dirname(__file__)
try:
import pip
except:
execfile(os.path.join(plugin_dir, get_pip.py))
import pip
# just in case the included version is old
pip.main(['install','--upgrade','pip'])
print('installed pip')
try:
import matplotlib
except:
import subprocess
print('installing matplotlib')
subprocess.call([sys.exec_prefix + '/python', "-m", 'pip', 'install', 'matplotlib'])
import matplotlib
print('installation completed')
try:
import keyring
except:
QgsMessageLog.logMessage(f"keyring not installed, installing keyring...","Messages", level=Qgis.Info)
import subprocess
subprocess.check_call(["python", "-m", "pip", "install", "--upgrade", "pip"])
subprocess.run(['pip', 'install', 'keyring'], check=True)
try:
import keyring
QgsMessageLog.logMessage(f"keyring installed.","Messages", level=Qgis.Info)
except:
QgsMessageLog.logMessage(f"keyring failed to be installed","Messages", level=Qgis.Critical)
return
Resources
QGis tutorial - Plugin development - part 1
QGis tutorial - Plugin development - part 2
QGis tutorial - Plugin development - part 3