Make your first Qgis plugin


Big steps

  1. To create your plugin... install plugins !
  2. Create an user interface (ui)
  3. Import your code and link it to your ui


Plugin builder

Help your plugin creation.

Gives an usable template, and an empty UI.

This is your first step to your first plugin.


You don't need anymore to reboot Qgis when you make a modification in your script.

How plugin works ?






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
  • Compile your ui in python (using command pyuic4)
         pyuic4 my_first_plugin_dialog_base.ui -o

2.Interface creation

Download PyQt4 (binary packages):

If you used plugin builder, change call for your ui (cf my post)

3. Link code/ui

Open your main (ie

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 ) :

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


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

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 :


    from osgeo import gdal
    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