1

I am developing a Plugin in QGIS and I want to show only selected parts of a layer. How can I handle this with PyQGIS?

4

I dont know whats possible in a plugin, but in QGIS you can try setting a filter like this:

lyr = QgsProject.instance().mapLayersByName('ak_riks_scaled')[0]
ids = [i.id() for i in lyr.getSelectedFeatures()] #List ids of selected features
if len(ids)==0:
    print('No selected features')
elif len(ids)==1:
    where_clause = '"fid" = {0}'.format(ids[0]) #Create a where clause to use as filter
    lyr.setSubsetString(where_clause)
else:
    where_clause = '"fid" IN{0}'.format(tuple(ids))
    lyr.setSubsetString(where_clause)

enter image description here

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