I've been having PyQt combo box issues for my QGIS 2.0, 2.2 plugin. I recently created another, albeit similar, thread (QCombo box to set layer (for Python QGIS plugin)) but realized that my problem surely surpassed the scope of the answer. I've tested a similar approach in a a simpler situation, but still have issues. I'd like to populate this combo box dynamically with layers in the QGIS legend, then set the active layer based on item choice. My problem is that the combo box only populates if the plugin is reloaded.
def initGui(self):
self.projCombo = QComboBox(self.iface.mainWindow())
self.projCombo.clear()
for layer in self.iface.legendInterface().layers():
if layer.type() == QgsMapLayer.VectorLayer:
self.projCombo.addItem(layer.name(), layer.id())
self.projComboAction = self.toolbar.addWidget(self.projCombo)
QObject.connect(self.projCombo,SIGNAL("currentIndexChanged(int)"),self.myActiveLayer)
def myActiveLayer(self, index):
registry = QgsMapLayerRegistry.instance()
activeID = str(self.projCombo.itemData(index))
self.activeLayer = registry.mapLayer(activeID)
self.iface.setActiveLayer(activeLayer)