I am a pyqgis user and I am trying to create a QGIS plugin where that plugin have two tabs widgets and two combo box where first combo box have a list from layers and second combo box have list of fields for current layer in combo box one.
That combo box is inside in tab widget two, and I want to to activate any time user start plugin.
My code works but not correctly - I have curious results.
In the first way in my _ init _ the combo box 1 work fine every time to start the user the plugin then combo box 1 activate correct, but the combo box 2 insert only six fields for some layers (that layers have many fields 19).
In the second way combo box 1 and combo box 2 work fine have correct list of layers and fields, but to activate combo box 1 must be the user to move some layers in panel and after this activate. I don't want this .
Where I have wrong ?
Who is correct signal activate for combo box 1 ?
Maybe some signal to activate when the user press tab 2 from tab widget?
in _ _init __
:
#first way
registry = QgsMapLayerRegistry.instance()
registry.layersAdded.connect(self.change_layers)
#second way
self.iface.legendInterface().groupRelationsChanged.connect(self.change_layers)
#field change
self.ui.comboBox_2.currentIndexChanged.connect(self.updateFields)
layer function :
def change_layers(self):
self.ui.comboBox_2.clear()
legendInterface = self.iface.legendInterface()
listLayerName = [i.name() for i in legendInterface.layers() if i.type() == QgsMapLayer.VectorLayer]
# add all these layer names to combo box comboLayerList
self.ui.comboBox_2.addItems(listLayerName)
field function :
def layer_field(self):
self.mapCanvas.refresh()
layers = self.iface.legendInterface().layers()
# Identify selected layer by its index
selectedLayerIndex = self.ui.comboBox_2.currentIndex()
print selectedLayerIndex
selectedLayer = layers[selectedLayerIndex]
# Identify fields of the selected layer
fields = selectedLayer.pendingFields()
# Get field names of the fields
fieldnames = [field.name() for field in fields]
# Clear comboBox_5
self.ui.comboBox_3.clear()
# Add field names to comboBox_5
self.ui.comboBox_3.addItems(fieldnames)