[QGIS-Developer] Python and ExpressionContext question

Andreas Neumann a.neumann at carto.net
Fri Dec 1 08:26:26 PST 2017


Hi, 

I am trying to evaluate an expression on a selected feature in my Python
code. 

Here is my code: 

----------------- 

selFeature = selectedFeatures[0]
context = QgsExpressionContext()
scope = QgsExpressionContextScope()
scope.setFeature(selFeature)
context.appendScope(scope)
exp = QgsExpression('represent_value(routenr)')
routenNameNr = exp.evaluate(context) 

------------------ 

The code above works fine for a "normal" expression, like e.g.
"$length", but it fails for "represent_value(fieldname)". I guess this
is because represent_value involves another layer (relation reference
widget). How could I enhance my code so that
"represent_value(fieldname)" would also work? 

Thanks a lot for your hints! 

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20171201/9ec161c3/attachment.html>


More information about the QGIS-Developer mailing list