0

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)
Community
  • 1
  • 1
user25976
  • 1,005
  • 4
  • 18
  • 39
  • So: are there any layers of the appropriate type available to populate the combo when `initGui` is called? And does the number of those layers change afterwards? – ekhumoro Apr 16 '14 at 21:02
  • To test this, i used the following code in the QGIS python console: for layer in iface.legendInterface().layers(): if layer.type() == QgsMapLayer.VectorLayer: print layer.name(). It prints all of my vector layer's names of type unicode. What do you mean by "does the number of those layers change afterwards?" – user25976 Apr 16 '14 at 21:56
  • You need to do the test when the `initGui` is called: doing it later doesn't really tell you anything. And on the other point: I mean, is the number of layers fixed? And also: exactly when and how do the layers get created? Obviously, if you call `initGui` before the layers are created, the combo-box is not going to get populated. – ekhumoro Apr 16 '14 at 22:39
  • Okay--added to my initGUI for layer in layers: if type == vector: layerName = layer.name(); QMessageBox.information(None, 'Add layer result', "Layer name: %s" % layerName). The message boxes pop up for my layers. The number of layers is not necessarily fixed, which is why i'd like to dynamically populate this combo box based on the layers existing in the stack. the layers are shapefiles previously added to the QGIS project, which is why legendInterface() returns them. initGui is surely called afterwards, because the layers exist even if the plugin isn't used. – user25976 Apr 16 '14 at 23:04
  • Is the code in your question the actual code you are running? Because it just doesn't seem believable that the combo doesn't get populated based on what you've said so far in your comments. – ekhumoro Apr 16 '14 at 23:18
  • my problem is actually different than i thought--i've modified my post – user25976 Apr 18 '14 at 01:01

0 Answers0