<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Still stumped with this, now cross-posted to
<a class="moz-txt-link-freetext" href="https://gis.stackexchange.com/questions/474014/changing-attributes-of-a-newly-created-feature-in-python-form-init-code">https://gis.stackexchange.com/questions/474014/changing-attributes-of-a-newly-created-feature-in-python-form-init-code</a></p>
<p>I noticed that the both the `id(feature)` is always different
when I look at it in either function which suggests the object got
copied (by the `partial()` call?). The same is true for `feature.id()`.
As a hack I tried using a global instead of passing the feature to
`on_ok()`. [insert confused emoji here]</p>
<p>Probably I am blind to something really basic.<br>
</p>
<p>Cheers, Hannes<br>
</p>
<div class="moz-cite-prefix">On 04.01.24 16:47, Johannes Kröger
(WhereGroup) via QGIS-Developer wrote:<br>
</div>
<blockquote type="cite"
cite="mid:e34a1900-2650-40bf-a3bd-27528d36d99b@wheregroup.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<p>Hey guys,</p>
<p>I feel really dumb and I guess I am missing some basic
concept...:</p>
<ul>
<li>I have a layer with two attributes: "text" (string) and
"int" (integer).</li>
<li>I used drag'n'drop form design to have a form with just the
"text" attribute in it.</li>
<li>I added Python Init Code:</li>
</ul>
<p>```<br>
from functools import partial<br>
<br>
def on_ok(layer, feature):<br>
field_idx = layer.fields().indexOf("int")<br>
attribute_changed = layer.changeAttributeValue(feature.id(),
field_idx, 12345)<br>
if not attribute_changed:<br>
raise Exception("Attribute value could not be changed!")<br>
<br>
def my_form_open(dialog, layer, feature):<br>
ok_button = dialog.findChild(QPushButton)<br>
ok_button.clicked.connect(partial(on_ok, layer, feature))<br>
```</p>
<p>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.</p>
<p>This works if I edit existing features.</p>
<p>But if I create a new feature, the "int" value will be
empty/NULL instead of 12345.</p>
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?
<p>Cheers, Hannes</p>
</blockquote>
</body>
</html>