4

I am working on a PyQGIS script where I allow the user to add features using the addFeature() map tool, where the user can create the geometry of the feature by clicking on the canvas, and after the geometry has been created, the user will be prompt up with a table where he can enter the attributes of the feature.

The question is, some attributes of the feature should be pre-defined, and some can be entered by the user. I found out that by using the following code I can suppress the prompt.

layer.editFormConfig().setSuppress(QgsEditFormConfig.SuppressOff)

But I still want the user to be able to enter most of the attributes after the new feature has been added, so it is not a good solution.

Is there another way where I can define some attributes after the feature has been added, and then prompt the table? In that way, the pre-defined value should be shown in the table when it is opened, so the user does not have to give value to them again.

Taras
  • 32,823
  • 4
  • 66
  • 137
Ending
  • 555
  • 2
  • 13

1 Answers1

1

There are a couple of options here by configuration or by script:

Script - use the layer changeAttributeValue() method to set values before the table opens, see How to change the value of an attribute using QgsFeature in PyQGIS?

Configuration - set layer default value through Properties, see How to set a default value for attributes in shapefiles?

artwork21
  • 35,114
  • 8
  • 66
  • 134