1

This question already has an answer here:

I am new to python and pyqqis.

I do not have the methods defined in initGUI

how do I do?

layers = self.iface.legendInterface().layers()
layer_list = []
for layer in layers:
    layer_list.append(layer.name())
self.dlg.comboBox.addItems(layer_list)

selectedLayerIndex = self.dlg.comboBox.currentIndex()
selectedLayer = layers[selectedLayerIndex]
fields = selectedLayer.pendingFields()
fieldnames = [field.name() for field in fields]
self.dlg.comboBox_2.addItems(fieldnames)

The problem is, that in second combobox there are fields from first on layerlist layer, not the selected one.

marked as duplicate by Midavalo Oct 20 '16 at 7:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Clear the items from 2nd combobox everytime before you fill it with fields of selected layer as below:

layers = self.iface.legendInterface().layers()
layer_list = []
for layer in layers:
    layer_list.append(layer.name())
self.dlg.comboBox.addItems(layer_list)

selectedLayerIndex = self.dlg.comboBox.currentIndex()
selectedLayer = layers[selectedLayerIndex]
fields = selectedLayer.pendingFields()
fieldnames = [field.name() for field in fields]
self.dlg.comboBox_2.clear()
self.dlg.comboBox_2.addItems(fieldnames)

Changes:
I have added clear() function before filling the combobox_2

  • Please pass me the complete code because it is not working – robson Oct 20 '16 at 18:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.