In the context of QGIS/PyQGIS, I would like to hide the unselected features after performing, for example, a simple selection.

feature = layer.getFeatures().next()

After running this commands a feature becomes highlighted. Besides, I'd like to hide the unselected features. How can I do that?

I believe this is possible because the Query Builder performs a filter that hides the undesired features.

  • The QgsVectorLayer method is 'deselect' and the parameter is the 'id' of selected feature.
    – xunilk
    Commented Oct 21, 2015 at 16:02

2 Answers 2


I just made it. In fact, I should use the setSubsetString method. Then I built a a query string inside a loop in order to look like this "gid IN ('8816','8836','8839','8864')". Then, when you run the setSubsetString method it hides the features that are not specified in the query string.

#Do any spacial query which returns some features
features = lyrPnts.getFeatures(QgsFeatureRequest().setFilterRect(geomPoly.boundingBox()))

#build the query string
strsel="gid IN ("
for feat in features:
    if feat.geometry().intersects(geomPoly):
        strsel=strsel+ "'" + str(feat.id()) + "',"

#Closes the string with a parenthesis
strsel=strsel[:-1] + ")"
#Hides the undesired features

Nice job



Features are controlled by the renderer, you cannot just "hide" a feature (unless you delete it). Instead you will have to setup a rule that matches a given property (in your case selection) to define the rendering parameters.

Have a look at the expressions+ plugin. There is a function called isselected(layername) which can be used in a style to control the transparency in any renderer or to only match the selected features in a rulebased renderer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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