Taking example code you posted at face-value, I can see several problems.
Firstly, judging by the differences between the initGui
and run
methods, there may be two sets of combo-boxes in use. The signals are connected to self.dlg.ui.*Combo
, whereas the items are added to self.dlg.*Combo
.
Secondly, you seem to be populating the combo-boxes over and over again without clearing them beforehand.
Thirdly, you do not seem to be preserving a one-to-one relationship between the combo-box indexes and the list, because you are filtering the layers based on type.
And finally, the list of layers comes from the values of a map, so surely there is no guarantee that they will come out in the same order.
I would suggest you associate a layer id with each combo item, and then retrieve the layer via the mapLayer method. That is, add the combo items like this:
self.dlg.indivCombo.addItem(layer.name(), layer.id())
and then retrieve the layer like this:
def layerChanged(self, index):
registry = QgsMapLayerRegistry.instance()
identifier = self.dlg.ui.indivCombo.itemData(index)
self.indivLayer = registry.mapLayer(identifier)
NB: if you're using Python2, the combo data will be stored as a QVariant
so you would need to extract the identifier like this:
identifier = self.dlg.ui.indivCombo.itemData(index).toString()
or this:
identifier = self.dlg.ui.indivCombo.itemData(index).toPyObject()