Tuesday, 14 April 2015

PyQGIS gives TypeError: QgsExpression.prepare(): argument 1 has unexpected type 'QgsFields'?


This question is very related to the unanswered Retrieve features from QgsFieldExpressionWidget pyqgis


I have a layer and I've build a QgsExpression that involves a field of that layer. What I'm trying to do is to evaluate the expression and get the results as a single list.


The problem is that I'm trying to do this with QGIS master (so the future 3 version) and the instruction of the PyQGIS cookbook are outdated because some API are changed.


Just to have an example:


vl = iface.activeLayer()
exp = QgsExpression('PH > 7') # where PH is the name of a field
exp.prepare(vl.pendingFields())



# then I get this error

Traceback (most recent call last):
File "/usr/lib/python3.5/code.py", line 91, in runcode
exec(code, self.locals)
File "", line 1, in
TypeError: QgsExpression.prepare(): argument 1 has unexpected type 'QgsFields'

Someone has already successfully used the QgsExpression for QGIS master?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...