<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi Roland,</p>
<p>I pasted your script into QGis-Version 3.38.1, and had some crashes, too, but not always.</p>
<p>But even if there was no crash, then the form-configuration afterwards had no fields. </p>
<p>I think, the problem could be, that you first store the tabs, than clear them and later use the previously stored but no more existing tabs to add them again.</p>
<p>Changing Your code to recreate a new tab instead of using references to cleared ones runs without crash and has the desired form-configuration: </p>
<p><font face="Courier New">...</font></p>
<font face="Courier New">fields_to_retain = ['field2']</font><br>
<font face="Courier New">edit_form_config = layer.editFormConfig()</font><br>
<font face="Courier New">edit_form_config.clearTabs()</font><br>
<font face="Courier New">for field in layer.fields():</font><br>
<font face="Courier New"> if field.name() in fields_to_retain:</font><br>
<font face="Courier New"> new_tab =
QgsAttributeEditorField(field.name(),layer.fields().indexOf(field.name()),None)</font><br>
<font face="Courier New"> edit_form_config.addTab(new_tab)</font><br>
<font face="Courier New">layer.setEditFormConfig(edit_form_config)</font>
<p><font face="Courier New">
</font></p>
<p>hth</p>
<p>Ludwig
</p>
<p><font face="Courier New">
</font></p>
<p>
</p>
<p>
</p>
<p></p>
<p></p>
<div class="moz-cite-prefix">Am 28.07.24 um 12:38 schrieb Roland
Berger via QGIS-User:<br>
</div>
<blockquote type="cite"
cite="mid:51ab1981-25a5-4526-929f-4d16503d0239@steinerpartner.com">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Hi<br>
<br>
I would like to achieve programmatically the equivalent of
removing a field from the drag an drop designer of a layers
"Attributes Form".<br>
Since there is no method to remove a field, the way to do it, is
to clearTabs() (
<a class="moz-txt-link-freetext"
href="https://qgis.org/pyqgis/3.34/core/QgsEditFormConfig.html#qgis.core.QgsEditFormConfig.clearTabs"
moz-do-not-send="true">https://qgis.org/pyqgis/3.34/core/QgsEditFormConfig.html#qgis.core.QgsEditFormConfig.clearTabs</a>
) and then to re-add the fields again. <br>
If I do that inside QGIS it crashes. If I do that with standalone
scripts I get a segmentation fault ([1] 4828 segmentation fault
(core dumped) ).<br>
Here is the code you can paste into qgis python console editor. It
will first setup a layer with 2 fields and then try to remove one
of the fields again. You can comment the last line to set thins up
first and rerun it with the uncommented line.<br>
<br>
So the question is:
<ol>
<li>Does QGIS crash in our environment too? If not, what is your environment and version in use?
</li>
<li>Did I miss something in the API?</li>
<li>Do I do it the wrong way?
</li>
<li>If yes, how to do it properly?</li>
<li>Is it a bug in QGIS or in the Python API?</li>
</ol>
Thanks<br>
Roland<br>
<br>
Paste this code into a QGIS Python Console Editor and run it:<br>
<pre># =================</pre>
<pre># Setup Layer with 2 fields</pre>
<pre># =================</pre>
<pre># Create a vector layer</pre>
<pre>layer = QgsVectorLayer("Point?crs=EPSG:4326", "MyLayer", "memory")</pre>
<pre>if not layer.isValid():</pre>
<pre> print("Failed to create the layer!")</pre>
<pre> sys.exit(1)</pre>
<pre># Add fields to the layer</pre>
<pre>layer_data_provider = layer.dataProvider()</pre>
<pre>layer_data_provider.addAttributes([QgsField("field1", QVariant.String), QgsField("field2", QVariant.String)])</pre>
<pre>layer.updateFields()</pre>
<pre># Add a feature to the layer</pre>
<pre>feature = QgsFeature(layer.fields())</pre>
<pre>feature.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(0, 0)))</pre>
<pre>feature.setAttribute("field1", "value1")</pre>
<pre>feature.setAttribute("field2", "value2")</pre>
<pre>layer_data_provider.addFeature(feature)</pre>
<pre>layer.updateExtents()</pre>
<pre># Get the current edit form configuration</pre>
<pre>edit_form_config = layer.editFormConfig()</pre>
<pre># Ensure the layout is set to use the tab layout</pre>
<pre>edit_form_config.setLayout(QgsEditFormConfig.TabLayout)</pre>
<pre># Apply the updated edit form configuration to the layer</pre>
<pre>layer.setEditFormConfig(edit_form_config)</pre>
<pre># Add the layer to the project</pre>
<pre>project = QgsProject.instance()</pre>
<pre>project.addMapLayer(layer)</pre>
<pre># =================</pre>
<pre># Remove field2 from tab layout</pre>
<pre># =================</pre>
<pre># List of fields to retain</pre>
<pre>fields_to_retain = ['field2']</pre>
<pre>edit_form_config = layer.editFormConfig()</pre>
<pre>tabs = edit_form_config.tabs()</pre>
<pre>edit_form_config.clearTabs()</pre>
<pre>for tab in tabs:</pre>
<pre> if tab.name() in fields_to_retain:</pre>
<pre> edit_form_config.addTab(tab)</pre>
<pre># layer.setEditFormConfig(edit_form_config) # --> QGIS Crashes after that in my case </pre>
<br>
<br>
<br>
The QGIS Version I use:<br>
<br>
<table
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;"
width="100%" cellspacing="2" cellpadding="0" border="0"
align="center">
<tbody>
<tr>
<td>
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">QGIS version</p>
</td>
<td>
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3.34.3-Prizren</p>
</td>
<td>
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">QGIS code revision</p>
</td>
<td>
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><a
href="https://github.com/qgis/QGIS/commit/47373234acd"
moz-do-not-send="true"><span
style=" text-decoration: underline; color:#0000ff;">47373234acd</span></a></p>
</td>
</tr>
<tr>
<td>
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Qt version</p>
</td>
<td colspan="3">
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">5.15.3</p>
</td>
</tr>
<tr>
<td>
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Python version</p>
</td>
<td colspan="3">
<p
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3.10.12</p>
</td>
</tr>
</tbody>
</table>
<style type="text/css">p, li { white-space: pre-wrap; }</style><br>
<br>
<br>
<br>
<br>
<div class="moz-signature"><br>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
QGIS-User mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-User@lists.osgeo.org">QGIS-User@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
</blockquote>
</body>
</html>