2

I am using QGIS 3.4.

I want to make attribute tables with many calculated fields that link to styles, and reuse the same structure for many projects.

For example, when I draw a pipe (line vector), and enter attributes like diameter and flow, these should calculate a range of derived values that affect how the style is presented on the map.

In a new project when I make a new geopackage layer and apply the "Irrigation Pipe" style, how can I make sure the field calculations are in the table and auto-generated?

I could make a function for each type of field calculation, but would I have to manually load the functions to each field every time I made a new attribute table?

QGIS novice here, it's been 20 years since I did lots of GIS!

1 Answer 1

3

The Attributes Form settings are saved in the QGIS style of the layer. There you can define an expression to default values for new and updated rows.

All the field widget (with the default expression) setting will be copied to a layer with the same name and type of field, when the QGIS style is loaded to that layer.

I have tried to refer to other values of the same row without problem with an expression like this:

attribute($currentfeature,'flow') / attribute($currentfeature, 'diameter')

Note that the expression will be evaluated if a row is created or updated, so if you want to auto populate existing rows you will need to do some trick: something like create a new dumb field and then update it from the Field Calculator with any random value, or write some script that update all the rows for you.

A last note: you will find a problem if you want to evaluate the default value on the provider side and the layer is not a Postgre SQL table. But if you are OK with QGIS evaluating the default value, the field widget expression works very well.

1
  • Thanks Gabriel, this helps a lot! Commented Jan 27, 2020 at 20:12

Your Answer

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

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