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()))
