10

Using QGIS 2.4 python console, I'm trying to display only a subset of a layer features. I've managed to get the features both from the layer :

featuresIterator = layer.getFeatures(QgsFeatureRequest().setFilterExpression ( u'"field"=4'))

and from the dataProvider :

featuresIterator = layer.dataProvider().getFeatures(QgsFeatureRequest().setFilterExpression ( u'"field"=4'))

(I'm not sure to understand the difference).

What I want to achieve now is to display only these features. In the exact same way as one would do using the query builder from the GUI. I don't know whether this has to be managed at the level of render (which would render some features and not others), or at the level of the data provider (which would only provide the layer with a view on the data containing only the wanted subset), or somewhere in between.

1
  • The answer from @Nathan is right. We can also get what is set by setSubsetString() or Query Builder using layer.subsetString(). If it's empty, then a empty string '' is returned. – Zac Feb 13 '20 at 4:06
14

The method you are looking for is called setSubsetString

layer.setSubsetString("field=4")
1
  • Thanks so much. The name of the function is not setSubString as you write it in you your answer, but setSubsetString. The link points to the good function anyway. I've edited your answer for future reference. Thanks again. – Jean-Philippe Nov 6 '14 at 19:34

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.