[QGIS-Developer] Changing attributes of a newly created feature in Python form init code
Frank Broniewski
broniewski at a-a.lu
Wed Jan 10 06:40:32 PST 2024
Hi Hannes,
i can unfortunately only confirm that this does not work, one can't seem to be able to set any field value (tried "text" as well). If you have the feature you can usually just do `feature["int"] = 12345` or feature.setAtrtribute("int", 12345), but that does not have any effect on this particular feature - and it does not raise an error either.
I checked the feature's field map, also the value in "text", all seems right. One thing I noticed is that all newly created features in my test had the same feature.id() of -9223372036854775808 which seems rather high, usually, unsaved features have negative values, but counting downwards from 0 - at least from my last experience with PostGIS layers ... mayby memory layers behave different here.
I wonder if the feature you get from the event is the real feature that is added to the layer or some kind of read-only copy of it ...
Grüße
Frank
________________________________
Von: QGIS-Developer <qgis-developer-bounces at lists.osgeo.org> im Auftrag von Johannes Kröger (WhereGroup) via QGIS-Developer <qgis-developer at lists.osgeo.org>
Gesendet: Donnerstag, 4. Januar 2024 16:47
An: qgis-developer <qgis-developer at lists.osgeo.org>
Betreff: [QGIS-Developer] Changing attributes of a newly created feature in Python form init code
Hey guys,
I feel really dumb and I guess I am missing some basic concept...:
* I have a layer with two attributes: "text" (string) and "int" (integer).
* I used drag'n'drop form design to have a form with just the "text" attribute in it.
* I added Python Init Code:
```
from functools import partial
def on_ok(layer, feature):
field_idx = layer.fields().indexOf("int")
attribute_changed = layer.changeAttributeValue(feature.id(), field_idx, 12345)
if not attribute_changed:
raise Exception("Attribute value could not be changed!")
def my_form_open(dialog, layer, feature):
ok_button = dialog.findChild(QPushButton)
ok_button.clicked.connect(partial(on_ok, layer, feature))
```
So the form does not show an input widget for the "int" field. But when pressing OK, the code should change the edited feature's "int" value to 12345.
This works if I edit existing features.
But if I create a new feature, the "int" value will be empty/NULL instead of 12345.
Any pointers on how to properly update a new feature's attribute fields, that exist on a layer but are not shown the user with the QGIS' widgets in the attribute form?
Cheers, Hannes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20240110/713c2491/attachment.htm>
More information about the QGIS-Developer
mailing list