<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
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">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"><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>
</body>
</html>