Make your first Qgis plugin
HOWTO

Big steps
- To create your plugin... install plugins !
- Create an user interface (ui)
- Import your code and link it to your ui
1.Plugins
Plugin builder
Help your plugin creation.
Gives an usable template, and an empty UI.
This is your first step to your first plugin.
Reloader
You don't need anymore to reboot Qgis when you make a modification in your script.
How plugin works ?
main file.py
interface.py
resources.py
interface.ui
resources.qrc
pyuic4
pyrcc4
Very simplified schema
2.Interface creation
Open Qt Designer
Load empty UI generated with Plugin Builder and add classes :
Class | Use for |
---|---|
QPushButton | Clic (run script, load file, save...) |
QLabel | Text or image |
QgsMapLayerComboBox | List of all raster open in Qgis |
QComboBox | List fields you want |
... | ... |
Once your ui is done :
- Open ui file as text and remove resources.qrc tag if exists (and if you use images)
- Compile resources.qrc in python (using pyrcc4 )
pyrcc4 resources.qrc -o resources.py - Compile your ui in python (using command pyuic4)
pyuic4 my_first_plugin_dialog_base.ui -o my_first_plugin_dialog_base.py
2.Interface creation
Download PyQt4 (binary packages):
https://www.riverbankcomputing.com/software/pyqt/download
If you used plugin builder, change call for your ui (cf my post)
3. Link code/ui
Open your main file.py (ie my_first_plugin.py)
In import PyQt4.QtGui add QDialog and QFileDialog :
QFileDialog is loading/saving window
QDialog is to know for where the action comes from
Add QDialog in your main class dependency, then init QDialog:
from PyQt4.QtGui import QDialog, QFileDialog
myfirstplugin( QDialog ) :
QDialog.__init__(self)
3. Link code/ui
Import QgsMessageLog for a perfect debugging
Then you create a log message with :
from qgis.core import QgsMessageLog
QgsMessageLog.logMessage('bug using : '+yourVariabletoTest)
Once QDialog is init, you can use self.sender()
It gives you the origin of an action because you can't use args
Example : you clic on the "runButton" and self.dlg is your ui() :
3. Link code/ui
self.dlg.runButton.clicked.connect(self.saveTo)
def saveTo(self):
if self.sender() == self.dlg.runButton:
#do the job
fileName = QFileDialog.getSaveFileName(self.dlg, \
"Select output file","","TIF (*.tif)")
Then you can open a window that let the user select the file to save :
Now, what to do with your filename ?
Save it in a QLineEdit field.
If your file outRaster is a QLineEdit field, you can show to the user his file choice :
3. Link code/ui
self.dlg.outRaster.setText(fileName)
outRasterVariable = self.dlg.outRaster.text()
Then you can assign it to a variable
Well done !
You have now all basics to make your own Qgis plugin.
Some tips :
try:
from osgeo import gdal
except:
QgsMessageLog.logMessage('Gdal is not available')
Remember to use QgsMessageLog to debbug
Make sure your script runs in python first, then merge it in your plugin
Use self.sender() from QDialog to replace args from ui
To avoid bugs, use try/except function
Qgis Plugin Tutorial
By Nicolaï Van Lennepkade
Qgis Plugin Tutorial
- 1,550