[QGIS-Developer] QgsExpression python pattern

Nyall Dawson nyall.dawson at gmail.com
Wed Jan 31 13:25:34 PST 2018


On 1 February 2018 at 05:45, Chris Crook <ccrook at linz.govt.nz> wrote:
> I am updating some plugins which use QgsExpression, and I'm wondering about the best way to use expressions in QGIS3.  It in the past I used a pattern like:
>
>   exp=QgsExpression(expressionText)
>   exp.prepare(fields)
>   request=QgsFeatureRequest()
>   request.setSubsetOfAttributes( expression.referencedColumns(), fields )
>   for feat in layer.getFeatures(request)
>       value=expression.evaluate(feat)
>
> However with QGIS3 this has clearly changed!  QgsExpression is not included in the QGIS3 API https://qgis.org/api/annotated.html.

That's a bug with the generated docs (fix incoming ) - it's still
around. BTW you may prefer the work-in-progress Python API docs here:
http://python.qgis.org/api/

> The prepare and referencedColumns are no longer defined. I did have a look at the C++ code for QgsExpression, but I find I'm still looking for guidance.
>
> I've searched the QEP issues  been able to locate a QEP, or other documentation.  Can anyone point me to some documentation or provide an example of current best practice please.

Best place to check first is the API breaks docs:

https://qgis.org/api/api_break.html

This details the changes in QgsExpression (amongst all the other classes).

Hope that helps!

Nyall


More information about the QGIS-Developer mailing list