Hi Etienne

Thanks. Worked!
Do you know of a good documentation explaining the Philosophie of the pyQgis API? I never know which approach I need to take to change/add information: Working directly on the object, when to use a data provider, when to call layer.startEditing() / layer.commitChanges() /  layer.updateField() and so on. Or in other words: From where do you know that I worked on a copy of the QgsField?

Regards
Roland

On 8/2/24 11:50, Etienne Trimaille wrote:
Hi,
You should use the method in QgsVectorLayer, otherwise, you are updating a "copy" of the QgsField :

for field in layer.fields():
     layer.setFieldConfigurationFlag(
        layer.fields().indexFromName(field.name <http://field.name>()), Qgis.FieldConfigurationFlag.HideFromWfs, True)

On GitHub.com, I often search for a piece of code to see existing plugin how they deal with a piece of code : https://github.com/search?q=FieldConfigurationFlag+language%3APython&type=code&l=Python <https://github.com/search?q=FieldConfigurationFlag+language%3APython&type=code&l=Python> There is only one script, as this API is quite new, but still nice to know for next time.


Le jeu. 1 août 2024 à 17:09, Roland Berger via QGIS-User <qgis-user@lists.osgeo.org> a écrit :

    Hi

    I try to set the configuration for a vector layer with python.
    Below is one of the many ways I tried to do it.
    Now I'm of out of ideas (chatgpt, intellij ai assistent and google as
    well) and would be thankful for a working example.

    Thanks Roland

    Code you can use in a Python Console Editor. First it creates a
    layer to
    use for the test and then tries to set the field configuration.

    # =================
    # Setup Layer with field
    # =================

    # 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)])
    layer.updateFields()

    # Add a feature to the layer
    feature = QgsFeature(layer.fields())
    feature.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(0, 0)))
    feature.setAttribute("field1", "value1")
    layer_data_provider.addFeature(feature)
    layer.updateExtents()

    # Add the layer to the project
    project = QgsProject.instance()
    project.addMapLayer(layer)

    # =================
    # Try to set field configuration
    # =================
    layer.startEditing()
    for field in layer.fields():
         config_flags = field.configurationFlags()
         config_flags |= Qgis.FieldConfigurationFlag.HideFromWfs
         field.setConfigurationFlags(config_flags)
    #layer.updateFields()
    layer.commitChanges()




    _______________________________________________
    QGIS-User mailing list
    QGIS-User@lists.osgeo.org
    List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
    Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


--

*Steiner & Partner Landschaftsarchitektur GmbH**
*Roland Berger - Geschäftsführer Stv.
Waisenhausstrasse 2
CH-3600 Thun
Mobile: +41 79 709 56 89 <tel:+41%2079%20709%2056%2089>
Tel: +41 33 335 76 53 <tel:+41%2033%20335%2076%2053>
www.steinerpartner.com <http://www.steinerpartner.com>
_______________________________________________
QGIS-User mailing list
QGIS-User@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to