1

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.

0

1 Answer 1

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

1
  • Please pass me the complete code because it is not working
    – robson
    Commented Oct 20, 2016 at 18:25

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