2

I try the new filter expression with QGIS 2.8. I want to filter only the selected objects on a layer in my map. My expression is:

import processing
from qgis.core import * 
vlayer = processing.getObject('mylayer')
selected_objects = vlayer.selectedFeatures()`

I save this expression but I can't open QGIS after.

Error: "AttributeError: 'NoneType' object has no attribute 'selectedFeatures"

2
  • It just means that vlayer is null (no layer)
    – gene
    Commented Mar 11, 2015 at 18:41
  • The layer exist and no problem in Python console Commented Mar 12, 2015 at 13:19

1 Answer 1

2

If you type in the Python Console these help commands:

help(processing.getObject)
Help on function getObject in module processing.tools.dataobjects:

getObject(uriorname)

help(QgsVectorLayer.select)
Help on built-in function select:

select(...)
    QgsVectorLayer.select(QgsRectangle, bool)
    QgsVectorLayer.select(int)
    QgsVectorLayer.select(unknown-type)

help(QgsVectorLayer.selectedFeatures)
Help on built-in function selectedFeatures:

selectedFeatures(...)
    QgsVectorLayer.selectedFeatures() -> list-of-QgsFeature

You can observe that if your 'uiorname' is correct you should first select the features with 'select'.

This code works well in my system:

import processing
from qgis.core import * 
vlayer = processing.getObject('/home/zeito/pyqgis_data/world_borders.shp')

list=[501, 967, 3171]

for item in list:
    vlayer.select(item)

selected_objects = vlayer.selectedFeatures()

print selected_objects

for this shapefile with selected features by index 501, 967 and 3171:

enter image description here

where printed references to selected features objects were:

execfile(u'/home/zeito/scriptspyqgis/selected.py'.encode('UTF-8'))
[<qgis._core.QgsFeature object at 0xc6f2b6c>, <qgis._core.QgsFeature object at 0xc6f2a94>, 
<qgis._core.QgsFeature object at 0xc6f2a4c>] 
1
  • I agree that it works the way you did it. But, did you try it in the new function editor in the expression filter (QGIS 2.8)? Can't open QGIS after save this expression) Commented Mar 12, 2015 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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