[QGIS-Developer] PyQGIS: add feature (+defaults/expressions), with Form
Richard Duivenvoorde
rdmailings at duif.net
Mon Nov 23 02:45:00 PST 2020
In the issue Alessandro pointed to:
QgsVectorLayerUtils.createFeature
https://qgis.org/pyqgis/master/core/QgsVectorLayerUtils.html?highlight=qgsvectorlayerutils#qgis.core.QgsVectorLayerUtils.createFeature
THAT one takes the expression context into account!
So:
Given an editable layer 'layer' and a valid 'geom":
feat = QgsVectorLayerUtils.createFeature(layer, geom, {}, layer.createExpressionContext() )
iface.openFeatureForm(layer, feat)
works!!
Thanks Alessandro,
Regards,
Richard Duivenvoorde
On 11/23/20 10:34 AM, Richard Duivenvoorde wrote:
> Hi Core Devs,
>
> I'm thinking about a solution for this
>
> https://github.com/qgis/QGIS/issues/40213
> (PyQGIS: iface.openFeatureForm() seems not to handle autofill fields)
>
> My conclusion is: the normal handling of default values or expression based attribute values, and the opening of the form dialog, are 'locked' in QgsFeatureAction::addFeature (glued together with the opening of the form):
>
> https://github.com/qgis/QGIS/blob/master/src/app/qgsfeatureaction.cpp#L168-L320
> I do see a way to fix it (as in: use "iface.openFeatureForm: in PyQGIS WITH the handling of default and expression values).
>
> I wonder if anybody has an opinion about moving part of this functionality to the vector layer.
> Talking about
> - setting defaults
> - setting expression based values
>
> So QgsFeatureAction::addFeature would be shrunken till:
> - layer.initAttributesForFeatures (or somethink like that)
> - open feature form...
>
> Is this OK for others? Or is the general opinion that you should copy all this to PyQGIS if you want to open the feature form?
>
> Thanks for any input.
>
> Regards,
>
> Richard Duivenvoordd
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
More information about the QGIS-Developer
mailing list