Something unexpected is happening within a function connected to pyqt combo box currentIndexChanged event. Two combo boxes are populated and connected to a function to fire upon index change. The function then retrieves specific information from the selected combo box item and populates two other combo boxes respectively.

While all comboboxes are eventually and correctly populated, something is still occurring at the wrong time as I get AttributeError: 'NoneType' object has no attribute pendingFields on the lines indicated in the layerChanged function when the plugin is initially run. After I exit out of the python error warning, the dialog shows up with all comboboxes properly populated. Each time the combobox is used and a new item is selected, the second combobox correctly populates with no errors.

I am wondering if the problem may be related to the moment when combobox items are added--perhaps the currentIndexChanged signal is firing before the first item is added, and therefore is undefined?

The run function upon opening the plugin:

def run(self):
    # show the dialog


    layers = QgsMapLayerRegistry.instance().mapLayers().values()

    for layer in layers:
        if layer.type() == QgsMapLayer.VectorLayer and layer.geometryType() == QGis.Line:
            self.dlg.ui.resCombo.addItem(layer.name(), layer)
        if layer.type() == QgsMapLayer.VectorLayer and layer.geometryType() == QGis.Point:
            self.dlg.ui.compteurCombo.addItem(layer.name(), layer)


    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result == 1:
        # do something useful (delete the line containing pass and
        # substitute with your code)

This is how the comboboxes are connected to the signal and the run function is called.

def initGui(self):
    # Create action that will start plugin configuration
    self.action = QAction(
        u"Calculer statistiques du réseau AEP", self.iface.mainWindow())
    # connect the action to the run method

    # Add toolbar button and menu item
    self.iface.addPluginToMenu(u"&statsAEP", self.action)


The function called upon currentIndexChanged:

def layerChanged(self):
    registry = QgsMapLayerRegistry.instance()
    self.comptLayer = self.dlg.ui.compteurCombo.itemData(self.dlg.ui.compteurCombo.currentIndex())
    self.resLayer = self.dlg.ui.resCombo.itemData(self.dlg.ui.resCombo.currentIndex())
    #QMessageBox.information(None, 'Connection result', "Res layer %s" % self.resLayer.name())
#Error occurs here
    comptField_names = [str(field.name()) for field in self.comptLayer.pendingFields()]
#Error also occurs here
    resField_names = [str(field.name()) for field in self.resLayer.pendingFields()]


    for field in comptField_names:

    for field in resField_names:

1 Answer 1


Some things to note:

  • You should a more recent style of connecting signals. E.g.:




    (Not sure this applies to QGIS 2.8)

  • In terms of the error you receive, you said "...when the plugin is initially run.". When the plugin is initialised, those comboboxes are likely to be empty which would explain why a NoneType or empty object has no attributes whatsoever. One method could be to use a try; except AttributeError loop so that when the error occurs, it can just be ignored using pass:

    def layerChanged(self):
            registry = QgsMapLayerRegistry.instance()
            # rest of your code
        except AttributeError:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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