[Qgis-user] removing fields from drag and drop designer (leads to segmentation fault)
Ludwig Kniprath
ludwig at kni-online.de
Sun Jul 28 08:00:18 PDT 2024
Hi Roland,
I pasted your script into QGis-Version 3.38.1, and had some crashes,
too, but not always.
But even if there was no crash, then the form-configuration afterwards
had no fields.
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.
Changing Your code to recreate a new tab instead of using references to
cleared ones runs without crash and has the desired form-configuration:
...
fields_to_retain = ['field2']
edit_form_config = layer.editFormConfig()
edit_form_config.clearTabs()
for field in layer.fields():
if field.name() in fields_to_retain:
new_tab =
QgsAttributeEditorField(field.name(),layer.fields().indexOf(field.name()),None)
edit_form_config.addTab(new_tab)
layer.setEditFormConfig(edit_form_config)
hth
Ludwig
Am 28.07.24 um 12:38 schrieb Roland Berger via QGIS-User:
> Hi
>
> I would like to achieve programmatically the equivalent of removing a
> field from the drag an drop designer of a layers "Attributes Form".
> Since there is no method to remove a field, the way to do it, is to
> clearTabs() (
> https://qgis.org/pyqgis/3.34/core/QgsEditFormConfig.html#qgis.core.QgsEditFormConfig.clearTabs
> ) and then to re-add the fields again.
> 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) ).
> 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.
>
> So the question is:
>
> 1. Does QGIS crash in our environment too? If not, what is your
> environment and version in use?
> 2. Did I miss something in the API?
> 3. Do I do it the wrong way?
> 4. If yes, how to do it properly?
> 5. Is it a bug in QGIS or in the Python API?
>
> Thanks
> Roland
>
> Paste this code into a QGIS Python Console Editor and run it:
> # =================
> # Setup Layer with 2 fields
> # =================
> # Create a vector layer
> layer = QgsVectorLayer("Point?crs=EPSG:4326", "MyLayer", "memory")
> if not layer.isValid():
> print("Failed to create the layer!")
> sys.exit(1)
> # Add fields to the layer
> layer_data_provider = layer.dataProvider()
> layer_data_provider.addAttributes([QgsField("field1", QVariant.String), QgsField("field2", QVariant.String)])
> layer.updateFields()
> # Add a feature to the layer
> feature = QgsFeature(layer.fields())
> feature.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(0, 0)))
> feature.setAttribute("field1", "value1")
> feature.setAttribute("field2", "value2")
> layer_data_provider.addFeature(feature)
> layer.updateExtents()
> # Get the current edit form configuration
> edit_form_config = layer.editFormConfig()
> # Ensure the layout is set to use the tab layout
> edit_form_config.setLayout(QgsEditFormConfig.TabLayout)
> # Apply the updated edit form configuration to the layer
> layer.setEditFormConfig(edit_form_config)
> # Add the layer to the project
> project = QgsProject.instance()
> project.addMapLayer(layer)
> # =================
> # Remove field2 from tab layout
> # =================
> # List of fields to retain
> fields_to_retain = ['field2']
> edit_form_config = layer.editFormConfig()
> tabs = edit_form_config.tabs()
> edit_form_config.clearTabs()
> for tab in tabs:
> if tab.name() in fields_to_retain:
> edit_form_config.addTab(tab)
> # layer.setEditFormConfig(edit_form_config) # --> QGIS Crashes after that in my case
>
>
>
> The QGIS Version I use:
>
> QGIS version
>
>
>
> 3.34.3-Prizren
>
>
>
> QGIS code revision
>
>
>
> 47373234acd <https://github.com/qgis/QGIS/commit/47373234acd>
>
> Qt version
>
>
>
> 5.15.3
>
> Python version
>
>
>
> 3.10.12
>
>
>
>
>
>
>
>
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org
> List info:https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20240728/6e31a9a3/attachment.htm>
More information about the QGIS-User
mailing list