Fragmentos de código

Esta sección cuenta con fragmentos de código para facilitar el desarrollo de complementos.

Cómo llamar a un método por un atajo de teclado

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

self.iface.unregisterMainWindowAction(self.keyAction)

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

Since QGIS 2.4 there is new layer tree API that allows direct access to the layer tree in the legend. Here is an example how to toggle visibility of the active layer

root = QgsProject.instance().layerTreeRoot()
node = root.findLayer(iface.activeLayer().id())
new_state = Qt.Checked if node.isVisible()==Qt.Unchecked else Qt.Unchecked
node.setVisible(new_state)

Cómo acceder a la tabla de atributos de los objetos espaciales seleccionados

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

self.changeValue(50)