highlight feature from feature id in qgis

by vaibhav Dhiman   Last Updated April 26, 2017 09:22 AM

i am making a qgis plugin and want to highlight a feature from feature id but the feature id must be taken from the textbox my code is below but this shows error like

it = cLayer.getFeatures(QgsFeatureRequest(expr))
AttributeError: 'NoneType' object has no attribute 'getFeatures'

    inputid = self.dlg.lineEdit.text()
    cLayer = self.iface.mapCanvas().currentLayer()
    expr = QgsExpression( "\"ID\"= 54")
    it = cLayer.getFeatures(QgsFeatureRequest(expr))
    ids = [i.id() for i in it]
    if i['ID'] == inputid:
        cLayer.setSelectedFeatures(i['ID'])

Thanks in advance



Answers 1


It seems that QGIS doesn't recognize the layer.

The line:

cLayer = self.iface.mapCanvas().currentLayer()

returns a QgsVectorLayer() object only if it is currently selected in the Layers Panel. Otherwise, a None is returned and the error you encountered.

I don't know the source of the input layer, but a general method for loading it would be:

cLayer = QgsVectorLayer('path to the vector layer', 'display name', 'ogr')
mgri
mgri
April 26, 2017 10:37 AM

Related Questions




How to use processing in a PyQT plugin in QGis

Updated July 19, 2017 15:22 PM

Make donut polygons with python in QGIS

Updated January 25, 2018 10:22 AM

Button won't work for plug-in in QGIS

Updated April 21, 2018 23:22 PM