2

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 cLayer.setSelectedFeatures(me) TypeError: expected string or buffer

    inputid = self.dlg.lineEdit.text()
    cLayer = self.iface.mapCanvas().currentLayer()
    query = '"ID" = {''}'.format(inputid)
    selection = cLayer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
    me = [k.id() for k in selection]
    cLayer.setSelectedFeatures(me)
  • 1
    I believe @mgri is correct in that you will need to select your layer from the Layers Panel to avoid that error. What about using a QgsMapLayerComboBox where the user can select from a list of available layers and then call this layer in your code? – Joseph Apr 26 '17 at 10:59
2

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')
  • thanks now m getting an error like:- cLayer.setSelectedFeatures(ids) TypeError: expected string or buffer – vaibhav Dhiman Apr 26 '17 at 11:25
  • @vaibhavDhiman, as you read from the Tour, comments are used to asking for more information or clarify a question or answer. The error you reported is referred to another issue (and you get a new error because the one from your original question was solved), so the best would be accepting my answer (if it helped you to find a solution) and asking a new separate question where you isolate this new specific error. – mgri Apr 26 '17 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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