12

I would like to add in a layer's field a default value. I mean, every time a new feature is created, that field would be automatically filled with the default value.

In my case the default value would be the @project_filename variable (project's file name).

I can't find this functionality anywhere.

Regards,

8

Since QGIS 2.18, go to the layer properties / field properties and set an expression (@project_filename in this case) as the default value.

Expression based default values

https://www.qgis.org/en/site/forusers/visualchangelog218/index.html#feature-client-side-default-field-values

  • 1
    In C++ API if I am not mistaken there is class QgsDefaultValue to do it programmatically. Is there python alternative to that? – shika-kun Nov 21 '17 at 6:40
  • If you ask this as a new question, I'll be happy to answer it. Just send me a reminder here ;) – Matthias Kuhn Nov 21 '17 at 8:13
10

You can define a function which adds the project filename as an attribute and connect this function with the event that adds features. You can use the following code, change the name of the field to whatever you choose (I used Name) and paste it into the Python Console. Now whenever you add a new feature, the field will be populated with the current project name:

import os

# Get project name
project = QgsProject.instance()
project_name = os.path.basename(project.fileName())

# Set active layer
layer = qgis.utils.iface.activeLayer()

# Define function to select added feature and add attribute to field "Name"
def update(featureAdded):
    idx = layer.fieldNameIndex('Name')
    layer.changeAttributeValue(featureAdded, idx, project_name)

# Connect "featureAdded" event to "select" function
layer.featureAdded.connect(update)

Result

  • 1
    Do you need to run the script everytime you edit the layer or the script remains stored in the qgis project? – Vale May 3 '16 at 13:41
  • 1
    @Vale - You can save the script as a macro which will be stored within the QGIS project. I will edit the post to include this =) – Joseph May 3 '16 at 13:47
  • 1
    Why do you need to select features Joseph? – Germán Carrillo May 3 '16 at 13:50
  • 3
    I don't think there's a need to post another question, as there would be too much overlap. You're actually close. I'd suggest you to avoid selecting features. What you get as parameter when the SIGNAL featureAdded is triggered, is actually a feature id. And that's all you need for calling changeAttributeValue. Into the update function you would just need 2 lines (using your variable names): idx = layer.fieldNameIndex('Name') and layer.changeAttributeValue(featureAdded, idx, project_name). – Germán Carrillo May 3 '16 at 14:09
  • 1
    @GermánCarrillo - That worked, wow so simple! Thank you for your great suggestion buddy! I learn something everyday =) – Joseph May 3 '16 at 14:21

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.