I want to retrieve attributes of selected objects on a layer with PyQGIS.
All example I see is for the active layer:
vlayer=qgis.utils.iface.activeLayer()
selected_bf = vlayer.selectedFeatures()
But, my features are not on the active layer. What is the way to say that vlayer is another layer but not the active layer?
I put this:
import processing
from qgis.core import *
vlayer = processing.getObject('mylayer')
selected_objects = vlayer.selectedFeatures()
as a filter expression in QGIS 2.8 but I can't open QGIS after.
Error: "AttributeError: 'NoneType' object has no attribute 'selectedFeatures'"
Best Answer
You can also reference a layer by it's name:
if your not sure of the name, you can look for it:
Note for QGIS 3.x: in QGIS3 you should replace
QgsMapLayerRegistry
byQgsProject
(see this answer).