There doesn't seem to be a way to directly find a feature object's parent layer or whether it's selected from a method in the QgsFeature class.
A similar approach to
vlayer.selectedFeatures() is to test whether the
feat.id() is in
vlayer.selectedFeaturesIds(). QgsFeatureIds are not unique values compared with other vector layers, only within their own layer.
Alternatively, you could start with
vlayer.selectedFeatures() and iterate over those features, instead of all features of the provider.
Yet another approach is to initially gather sets (or lists) of selected and non-selected feature ids for a given vector layer:
# previous relevant code
set_selids = set(vlayer.selectedFeaturesIds())
feat = QgsFeature()
vlayer.select(, QgsRectangle(), False)
set_allids = set()
set_notselids = set_allids - set_selids
I can't seem to find a single call for retrieving a reference to all features (or ids) for a vector layer (i.e. still have to use
QgsVectorLayer.select() and iterate with
I updated the code to reflect QgsVectorLaer can handle select call (no need to get provider directly), and doesn't mess up actual selected features in map canvas, which would require
setSelectedFeatures() to update.
After building feature id sets, you can iterate over them and use
QgsVectorLayer.featureAtId(featid) to access the feature.