I have started using the QGIS Bézier Editing Plugin by Takayuki Mizutani v.1.3.4. Plotting line features in my project works well, but on saving the feature the attributes dialog is empty, whereas when I save a feature via the standard procedure, the attribute rules provide a new id-number from the database. Which pyqgis function applies the attribute rules to a new feature ? My QGIS version is 3.4.11-Madeira.

** Partial answer **

Playing with the excellent and necessary plugin IPython to inspect the data model, I found this Python expression: testlayer.fields().field(0).defaultValueDefinition.expression() which evaluates to 'if(count(@layer_id)=0,1,maximum("id")+1)' which is the default id number expression I am looking for. Then I grepped the codebase to search for "expression" and found the class QgsExpression. It can be instantiated with the string representation of the above expression, which is automatically parsed on init. You can query the hasParserErrors property or the isValid property to check all is right.

But then you are on your own again. The expression class has a method evaluate which should return the new id value, but it needs a QgsExpressionContext to provide the expression with the attributes of the testlayer features to calculate its result. The constructor of the expression context class only has a QgsExpressionContextScope stack as arguments. There also exists a QgsExpressionContextGenerator abstract interface, which should be implemented by the user.

So my question is narrowed down to how does the standard new feature dialog for LineString features with among others an id attribute create this expression context ?

  • Have you created any attributes on the layer? If you have no attributes, it would be no wonder the dialog is empty. You should add more information and add a screenshot so that we know what you have. Otherwise it's difficult to help.
    – Babel
    Sep 8, 2022 at 12:28

1 Answer 1


Basis: see the ** Partial Answer ** section in my edited question.

After more studying of the C++ source I found this class: QgsVectorLayerUtils. It has a method createFeature which honors all constraints the user has set on the vector layer in question. You just give it the vector layer as argument, and you receive a blank new feature on which all defaults are set.

You then copy the geometry from the feature the plugin has constructed and set the new feature in the feature attributes dialog of the plugin, so you can add the new attributes for the feature. The feature attributes dialog is provided by the vector layer, and applies the constraints.

Your Answer

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

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