1

I am developing a plugin that counts the number of features for a layer in QGIS 2.6. The plugin works fine when there is a layer but when there is no layer, it throws an error. For reference, a part of the code is also attached.

def run(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()

    layers = QgsMapLayerRegistry.instance().mapLayers().values()
    for layer in layers:
        if layer.type() == QgsMapLayer.VectorLayer:
            self.dlg.featurecombo.addItem( layer.name(), layer )
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result  == 1:
        index = self.dlg.featurecombo.currentIndex()
        layer = self.dlg.featurecombo.itemData(index)
        if layer > 0:
            QMessageBox.information(self.iface.mainWindow(), "feature count","%s has %d features." %(layer.name(), layer.featureCount()))

enter image description here

2
  • Your code is indicating that "name" does not exist in the object you are looping through. Try: import pprint and then in your loop: pprint.pprint(layer). This should help verify that "name" exists. Does it? Commented Jan 6, 2015 at 20:40
  • I made a change in the looping when there is no layer. That works. Thanks for the response. Commented Jan 7, 2015 at 5:02

1 Answer 1

2

Your code is indicating that "name" does not exist in the object you are looping through. Try: import pprint and then in your loop: pprint.pprint(layer).

If layer does not exist, then you will know what is causing your error and what items in your object are available for parsing.

Stay curious my friend.

1
  • Yeah edited the code and now it works fine.Thanks. Commented Jan 7, 2015 at 12:31

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.