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()
while vlayer.nextFeature(feat):
set_allids.add(feat.id())
set_notselids = set_allids - set_selids
print set_allids
print set_selids
print set_notselids
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 QgsVectorLayer.nextFeature()
).
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.
You can create and run various actions from the Attribute Table for each feature. If you go to Layer Properties > Actions
and add a new action, there is an option which allows you to run the action from the attribute table:
Once you have created an action, you can choose to show it as a combo box:
Now when you open your attribute table, you can see a new field containing the actions:
If you want your action to be run on multiple selected features, you will need to include this into your action (if possible). In my example, I am using Python to print the values from the first field for all selected features. So if I select multiple features and then click on any action combo box, it will print the values of the selected features:
However, there seems to be a Run layer action
when you right-click an attribute but unfortunately, I can't seem to find much information concerning this. I have posted this as a new question as I am also curious:
What is this 'Run layer action' and how to use it?
Best Answer
You can use the solutions given in Using processing algorithms from the console:
But if you look at what's in the module (version 2.2):
no more
getfeatures()
orgetFeatures()
You can control this with a little function adapted from Script de Python para filtrar por patrón de texto los métodos de Clases en PyQGIS de José Guerrero
but there is:
So the command is:
But for me, it is easiest with:
without processing:
with processing:
All the features:
Selected features only:
and, for example, an "universal solution":