[QGIS-Developer] PyQGIS: add feature (+defaults/expressions), with Form

C Hamilton adenaculture at gmail.com
Mon Nov 23 09:47:19 PST 2020


So why is there a separate way to create features? Should these be cross
referenced in the documentation as to why one method should be used vs. the
other? This solves the openFeatureForm problem. Perhaps the documentation
should also mention that if you use openFeatureForm then you need to
use QgsVectorLayerUtils.createFeature(...).

Thanks,

Calvin

On Mon, Nov 23, 2020 at 5:45 AM Richard Duivenvoorde <rdmailings at duif.net>
wrote:

> 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
> >
>
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20201123/af3fa048/attachment.html>


More information about the QGIS-Developer mailing list