Outdated version of the documentation. Find the latest one here.

Code Snippets

This section features code snippets to facilitate plugin development.

How to call a method by a key shortcut

In the plug-in add to the initGui():

self.keyAction = QAction("Test Plugin", self.iface.mainWindow())
self.iface.registerMainWindowAction(self.keyAction, "F7") # action1 triggered by F7 key
self.iface.addPluginToMenu("&Test plugins", self.keyAction)
QObject.connect(self.keyAction, SIGNAL("triggered()"),self.keyActionF7)

To unload() add:


The method that is called when F7 is pressed:

def keyActionF7(self):
  QMessageBox.information(self.iface.mainWindow(),"Ok", "You pressed F7")

How to toggle Layers (work around)

As there is currently no method to directly access the layers in the legend, here is a workaround how to toggle the layers using layer transparency:

def toggleLayer(self, lyrNr):
  lyr = self.iface.mapCanvas().layer(lyrNr)
  if lyr:
    cTran = lyr.getTransparency()
    lyr.setTransparency(0 if cTran > 100 else 255)

The method requires the layer number (0 being the top most) and can be called by:


How to access attribute table of selected features

def changeValue(self, value):
  layer = self.iface.activeLayer()
    nF = layer.selectedFeatureCount()
    if (nF > 0):
    ob = layer.selectedFeaturesIds()
    b = QVariant(value)
    if (nF > 1):
      for i in ob:
      layer.changeAttributeValue(int(i),1,b) # 1 being the second column
      layer.changeAttributeValue(int(ob[0]),1,b) # 1 being the second column
      QMessageBox.critical(self.iface.mainWindow(),"Error", "Please select at \
        least one feature from current layer")
    QMessageBox.critical(self.iface.mainWindow(),"Error","Please select a layer")

The method requires one parameter (the new value for the attribute field of the selected feature(s)) and can be called by: