<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    Hi Etienne<br>
    <br>
    Thanks. Worked!<br>
    Do you know of a good documentation explaining the Philosophie of
    the pyQgis API?<br>
    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 <font face="Courier New, Courier, monospace">layer.startEditing()
      / layer.commitChanges()</font> /  layer.updateField() and so on.<br>
    Or in other words: From where do you know that I worked on a copy of
    the QgsField?<br>
    <br>
    Regards<br>
    Roland<br>
    <br>
    <div class="moz-cite-prefix">On 8/2/24 11:50, Etienne Trimaille
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAMtDFLLS6p_O2ydmNBehzszG8UDEFy4NR8GG-aWDeHk52hDbHg@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div>Hi,</div>
        <div>You should use the method in QgsVectorLayer, otherwise, you
          are updating a "copy" of the QgsField :</div>
        <div><br>
        </div>
        <div>for field in layer.fields():<br>
               layer.setFieldConfigurationFlag(<br>
                  layer.fields().indexFromName(<a
            href="http://field.name" moz-do-not-send="true">field.name</a>()),
          Qgis.FieldConfigurationFlag.HideFromWfs, True)</div>
        <div><br>
        </div>
        <div>On GitHub.com, I often search for a piece of code to see
          existing plugin how they deal with a piece of code : <br>
        </div>
        <div><a
href="https://github.com/search?q=FieldConfigurationFlag+language%3APython&type=code&l=Python"
            moz-do-not-send="true">https://github.com/search?q=FieldConfigurationFlag+language%3APython&type=code&l=Python</a></div>
        <div>There is only one script, as this API is quite new, but
          still nice to know for next time.<br>
        </div>
        <div><br>
        </div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">Le jeu. 1 août 2024 à 17:09,
          Roland Berger via QGIS-User <<a
            href="mailto:qgis-user@lists.osgeo.org"
            moz-do-not-send="true" class="moz-txt-link-freetext">qgis-user@lists.osgeo.org</a>>
          a écrit :<br>
        </div>
        <blockquote class="gmail_quote"
style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi<br>
          <br>
          I try to set the configuration for a vector layer with python.<br>
          Below is one of the many ways I tried to do it.<br>
          Now I'm of out of ideas (chatgpt, intellij ai assistent and
          google as <br>
          well) and would be thankful for a working example.<br>
          <br>
          Thanks Roland<br>
          <br>
          Code you can use in a Python Console Editor. First it creates
          a layer to <br>
          use for the test and then tries to set the field
          configuration.<br>
          <br>
          # =================<br>
          # Setup Layer with field<br>
          # =================<br>
          <br>
          # Create a vector layer<br>
          layer = QgsVectorLayer("Point?crs=EPSG:4326", "MyLayer",
          "memory")<br>
          if not layer.isValid():<br>
               print("Failed to create the layer!")<br>
               sys.exit(1)<br>
          <br>
          # Add fields to the layer<br>
          layer_data_provider = layer.dataProvider()<br>
          layer_data_provider.addAttributes([QgsField("field1",
          QVariant.String)])<br>
          layer.updateFields()<br>
          <br>
          # Add a feature to the layer<br>
          feature = QgsFeature(layer.fields())<br>
          feature.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(0, 0)))<br>
          feature.setAttribute("field1", "value1")<br>
          layer_data_provider.addFeature(feature)<br>
          layer.updateExtents()<br>
          <br>
          # Add the layer to the project<br>
          project = QgsProject.instance()<br>
          project.addMapLayer(layer)<br>
          <br>
          # =================<br>
          # Try to set field configuration<br>
          # =================<br>
          layer.startEditing()<br>
          for field in layer.fields():<br>
               config_flags = field.configurationFlags()<br>
               config_flags |= Qgis.FieldConfigurationFlag.HideFromWfs<br>
               field.setConfigurationFlags(config_flags)<br>
          #layer.updateFields()<br>
          layer.commitChanges()<br>
          <br>
          <br>
          <br>
          <br>
          _______________________________________________<br>
          QGIS-User mailing list<br>
          <a href="mailto:QGIS-User@lists.osgeo.org" target="_blank"
            moz-do-not-send="true" class="moz-txt-link-freetext">QGIS-User@lists.osgeo.org</a><br>
          List info: <a
            href="https://lists.osgeo.org/mailman/listinfo/qgis-user"
            rel="noreferrer" target="_blank" moz-do-not-send="true"
            class="moz-txt-link-freetext">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
          Unsubscribe: <a
            href="https://lists.osgeo.org/mailman/listinfo/qgis-user"
            rel="noreferrer" target="_blank" moz-do-not-send="true"
            class="moz-txt-link-freetext">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
        </blockquote>
      </div>
    </blockquote>
    <br>
    <div class="moz-signature">-- <br>
      <p><b><span style="font-size:10pt;color:rgb(187,145,55)">Steiner
            & Partner Landschaftsarchitektur GmbH</span></b><b><span
            style="font-size:10pt"><br>
          </span></b><span style="font-size:10pt;color:rgb(127,127,127)">Roland
          Berger - Geschäftsführer Stv.<br>
          Waisenhausstrasse 2<br>
          CH-3600 Thun<br>
          Mobile: <a href="tel:+41%2079%20709%2056%2089"
            value="+41797095689" target="_blank">+41 79 709 56 89</a><br>
          Tel: <a href="tel:+41%2033%20335%2076%2053"
            value="+41333357653" target="_blank">+41 33 335 76 53</a><br>
          <a href="http://www.steinerpartner.com" target="_blank">www.steinerpartner.com</a></span></p>
    </div>
  </body>
</html>