jeudi 26 février 2015

Warning message needed when there is no specific layer instead of python error


In my plugin, when I click a button it will call the findTheCityNames function which is attached below and get features if there is a particular layer. What I want is if there is no specific layer in which it is getting features, it should give a warning message "No Valid layer found" instead python error "List object has no attribute getFeatures".



def findTheCityNames(self):
self.dlg.Citycombo.clear()
ct = self.dlg.Statecombo.currentText()
st = "state='"+ct+"'"
exp = QgsExpression(st)
fidAndVal = {}
#QMessageBox.information(self.dlg, "info", st)

layer = self.iface.activeLayer()

iterFeat = layer.getFeatures()

for f in iterFeat:
val = exp.evaluate(f)
if bool(val):
attrs = f.attributes()
idx = layer.fieldNameIndex('name')
cityName = f.attributes()[idx]
fidInt = f.id()
fidAndVal[fidInt] = cityName

for x in fidAndVal.keys():
self.dlg.Citycombo.addItem(fidAndVal[x])




Aucun commentaire:

Enregistrer un commentaire