I came across with a problem. I don't know how to change active layer by using a combo box object (QComboBox). I manage to create a list of names but I don't know how to change active layer in QGIS plugin Combo Box yet.
index = QgsMapLayerRegistry.instance().mapLayers().values()
for i in index:
if i.type() == QgsMapLayer.VectorLayer:
self.dockwidget.active.addItem( i.name(), i )
Above code creates a list of my layers, but I can not change active layer with this method. Whenever active layer is changed, combo box should also update its reference.
Can you help me? I am using Qgis 2.16.
Best Answer
In this case it's preferable to use directly a QgsMapLayerComboBox object. I's very easy. For example, in next situation with 4 layers:
at the Python Console of QGIS I can do:
Selecting 'grid' layer (see next image):
next command prints correctly the name of selected layer because selection was automatic:
as it can also be observed at next image:
At the plugin you need:
Editing Note:
With your code is:
and produces this:
Afterwards (equivalent to click in OK button), selecting 'grid' layer: