Qgis: PyQGIS: iface.openFeatureForm() seems not to handle autofill fields?

Created on 21 Nov 2020  路  3Comments  路  Source: qgis/QGIS

Coming from
https://github.com/NationalSecurityAgency/qgis-latlontools-plugin/issues/52

Where the issue is: creating a feature+geometry in PyQGIS, and then open the featureform does not init the (autocreation) fields.
OR we miss an essential step in the creation of the feature.

To see it:

  • open the project in the zip:
    auto.zip
  • note that the 'text' field is created with an expression function to autogenerate the value with uuid()

Screenshot-20201121113827-1128x669

  • enable editing on the layer 'auto'
  • use the normal digitize tools to add a point and digitize a point. You will see:
    Screenshot-20201121114300-472x178
    Qgis promisses to create the fid (gpkg) upon creation, AND it autofills the text field with the expression result of 'uuid()'

  • now use the same project to try the same via PyQGIS:

x = 10
y = 10
# make sure the 'auto' layer is selected and enabled for edit
layer = iface.mapCanvas().currentLayer()

feat = QgsFeature()
feat.setFields(layer.fields())
feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(x, y)))
# thinking this is maybe needed: (but does not help)
feat.initAttributes(fieldCount=2)
print(f'feat.isValid: {feat.isValid()}') # it is valid now
iface.openFeatureForm(layer, feat)

You will see the following:

Screenshot-20201121114443-472x178

1) fid is apparently not rightly initialized (? red cross)
2) expressions are not ran yet apparently because 'text' field is still zero.

I tried to debug this but could not yet find the issue.
OR are we just not creating the QgsFeature in the right way/order?

Bug

All 3 comments

The handling of the expressions during a normal digitize go through:

https://github.com/qgis/QGIS/blob/master/src/app/qgsfeatureaction.cpp#L168-L320

Anybody an idea how to hit this using PyQGIS? Following is not possible:

action = QgsFeatureAction("add feature", feat, layer, QString(), -1, iface );
action.addFeature( QgsAttributeMap(), showModal, scope )

@rduivenvoorde did you try to create the feature with QgsVectorLayerUtils::createFeature ?

     * Creates a new feature ready for insertion into a layer. Default values and constraints
     * (e.g., unique constraints) will automatically be handled. An optional attribute map can be
     * passed for the new feature to copy as many attribute values as possible from the map,
     * assuming that they respect the layer's constraints. Note that the created feature is not
     * automatically inserted into the layer.
     * \see createFeatures()

@elpaso Ah, thanks I was not aware of that...

https://qgis.org/pyqgis/master/core/QgsVectorLayerUtils.html?highlight=qgsvectorlayerutils#qgis.core.QgsVectorLayerUtils.createFeature

Trying now. Only issue I have with these "' context'-based params " is where to get these from ...

Ah, this one works:

feat = QgsVectorLayerUtils.createFeature(layer, geom, {}, layer.createExpressionContext() )

(earlier not, because I did an 'initFeatures(..)' after it...

Thanks!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Saijin-Naib picture Saijin-Naib  路  3Comments

aborruso picture aborruso  路  4Comments

hshatti picture hshatti  路  3Comments

Saijin-Naib picture Saijin-Naib  路  4Comments

pathmapper picture pathmapper  路  3Comments