2

I would like to create and populate a virtual field in a QGIS vector layer attribute table using a python script. I think the easiest way to do this is to create a custom expression function, and, then use this function to populate the virtual field. How do I instantiate a custom expression function from within a python script? What I have tried with Python 3 from a Python IDE is:

    from qgis.core import *

@qgsfunction(args=0, group='Custom', usesGeometry=False)
def a_function():
    return "'Test'"

QgsExpression.registerFunction(a_function)
print(QgsExpression(a_function()).evaluate())

This returns a TypeError:

print(QgsExpression(a_function()).evaluate())
TypeError: 'QgsPyExpressionFunction' object is not callable
1

The following works, but, I don't know why:

from qgis.core import *
@qgsfunction(args='auto', group='Custom', usesGeometry=False)
def a_function(feature, parent):
    return 'Test'
print(QgsExpression('a_function()').evaluate())
-1

Try this

from qgis.core import *

@qgsfunction(args=0, group='Custom', usesGeometry=False)
def a_function():

    QgsExpression.registerFunction(a_function)
    print(QgsExpression(a_function()).evaluate())
    return "'Test'"
  • I would like to call the function in an expression. In my example, the output of print(QgsExpression(a_function()).evaluate()) should be the string 'Test'. . In your provided answer the statement print(QgsExpression(a_function()).evaluate()) does not return anything. – Philip Whitten Jun 3 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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