I'm trying to write a plugin for QGIS that adds a field to a layer selected from a combobox and calculates area.
For that I import these modules:
from PyQt4.QtCore import QVariant, QSettings, QTranslator, qVersion, QCoreApplication
from PyQt4.QtGui import QAction, QIcon, QFileDialog
and in the run function:
def run(self):
# here I populate a combo box in a form
layers = self.iface.legendInterface().layers()
layer_list = []
for layer in layers:
layer_list.append(layer.name())
self.dlg.Cmb_Capa.addItems(layer_list)
self.dlg.show()
result = self.dlg.exec_()
if result:
Now here comes my problem… I find this in the documentation page of QGIS to add a field to a layer
if caps & QgsVectorDataProvider.AddAttributes:
res = layer.dataProvider().addAttributes([QgsField("mytext", QVariant.String), QgsField("myint", QVariant.Int)])
but I don't know how to take the layer selected in the combo and apply this.
Can anyone give me a hand or a tip about how to do it?
Best Answer
One method (which you are currently using) is the standard QComboBox. First we need to import some more classes so add this before importing PyQt4:
Then in your
run(self)
function, we can use the following (I've added some comments which hopefully will help you understand what's going on):Alternatively, you could use QGIS custom widget: QgsMapLayerComboBox. This is automatically updated when any changes are made to the layer list (i.e. layers added/removed, names changed etc) without you having to refresh the plugin. To use this, you will need to import an additional class:
Now your
run(self)
function can look like this:Note: When using QGIS custom widgets, you may need to edit your "plugin_name_dialog_base.ui" file, find the
QgsMapLayerComboBox class
and replace the<header>
to qgis.gui.h.For example, it might look like this:
And it needs to look like this: