[GIS] Can not Run function when item selected in comboBox. Pyqgis. QGIS

pyqgispyqtpythonqgis

I'm writing a plugin, I want to change the content of a comboBox (fields) depending of the content of another comboBox (layers).

My plugin is a dockwidget. I can populate the fields comboBox just starting the plugin in the def run(self): function, and it's working fine:

        layerNom = self.dockwidget.comboBoxErrores.currentText()#Get the text value of the comboBox
        for lyr in QgsMapLayerRegistry.instance().mapLayers().values():    #Get layers        
            if lyr.name()==str(layerNom): 
                fields = lyr.pendingFields() #Get Fiels
                field_names = [field.name() for field in fields] #Field List
                self.dockwidget.comboCampos.addItems(field_names) #Added to the comboBox

But when I try to update the content of the fields comboBox I really don't know how to do it.

In my "pluginName_dockwidget.py" file, in the init function I have created the connection with the function that will repopulate the comboBox with the new content:

self.comboBoxErrores.currentIndexChanged['QString'].connect(self.pueblaCampos)

In the same file I have a function that get the value of the layer name of the comboBox and emit it with the signal created previously:

sennalComboIndex = pyqtSignal(str)
.
.
.
def pueblaCampos(self):      

    layerName = self.comboBoxErrores.currentText()
    self.sennalComboIndex.emit(layerName)

In my main file, I have the rest of the function:

def pueblaCampos(self, layerName):        

    for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
        if lyr.name()==str(layerName):
            fields = lyr.pendingFields()
            field_names = [field.name() for field in fields]             
            self.dockwidget.comboCampos.clear()
            self.dockwidget.comboCampos.addItems(field_names)

When I change the slected item in the layers comboBox, nothing happens. What Am I missing?

Best Answer

As I noted in my comment, add your .connect statements to the initGui() function. This will allow the signal to be heard on whatever connect event.