I wan't to filter features of a
Shapefile layer from a user's selection with
PyQGIS. When I select less than 30 features with a method like
fid=1 or fid=2 or fid=3 ..., everything works fine but when I select over 30 features filter doesn't work and return me all the features of the layer. If I try a filter like
fid >= 1 and fid <=150 (for example), it works. But it doesn't help for lists of unfollowing ids. I'm using
Is there something wrong with this ? Does filters have size limits ? Is there a better way to filter features ?
# Layer layer = iface.activeLayer() features = layer.selectedFeatures() # List ID of selected features l =  for feature in features: l.append(feature.id()) # Build arguments for the query index = 0 query =  prefix = 'fid=' while index < len(l)-1: query.append(prefix) query.append(l[index]) query.append(' or ') index+=1 # Last element of the list query.append(prefix) query.append(l[index]) # Query fullQuery = str(query).strip('').replace(',', '').replace("'", "") # Filter layer.setSubsetString(fullQuery)