Calculating attribute with formula in QGIS
see update below
Use the field calculator, create the expression by adding all the fields together. Field names can be double clicked from the middle widget in the calculator window, under the heading "Felder und Werte" (don't know how its called in english, probably "Attributes and Values"). They will then be added to the expression, and you only have to manually key in the "+" for addition.
Method for form editor, not using the attribute table calculation:
this works only for QGIS 3.10 and later
The trick is to just input the formula into the expression box. It doesn't show the fields when clicking on "Felder und Werte", but the expression works nevertheless! Then it was just a matter of randomly clicking and trying out different options to make the field update and retain the default value when clicking OK and not having made an edit to the calculated value.
A catch is to check "Änderbar" at the top of the form, under the field name. If the field is not Änderbar (changeable), then the calculated value will be displayed but not written to the attribute table!
The "default value" setting ("Vorgabewert" in your QGIS language setting) cannot use current field values in its calculation. That would be a great feature* if it did, but it doesn't. It can only use values like $area
or $length
.
So instead you have to use a workaround like:
- As the other answers suggested, re-calculate the field with the field calculator after creating the feature.
- Use a virtual field. Note that a virtual field is only stored in the project, it's not stored in the source data.
*Update: it sounds like this feature was introduced in QGIS 3.10 (see the changelog for details). So this answer applies to QGIS versions prior to 3.10. Update to the latest version if you want default field values that depend on other field values.
You should be able to set the field value itself to a function of other values, just like in Excel etc. You can do this in the layer properties in section Attribute Form. Just select the field, you want to change, click on the little Sigma-Icon under Expression and type in your expression (see below). It should actually be possible with your values.