<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
Hi Hannes and Ujaval<br>
<br>
I found the problem. After adding a virtual field I also loaded
style from a qml file for that layer:<br>
<pre>layer.loadNamedStyle("mystyle.qml", QgsMapLayer.<i>Symbology</i>) </pre>
Looks like even when I only load the stye for the <i>Symbology</i>
also the <i>Fields</i> settings are somehow changed/overwritten.<br>
<br>
Thanks<br>
Roland<br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 7/26/24 16:39, Johannes Kröger
(WhereGroup) via QGIS-User wrote:<br>
</div>
<blockquote type="cite"
cite="mid:3ae20a76-6f47-41bf-98d4-c6f563485bde@wheregroup.com">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<p>QGIS' virtual fields (expression fields) are not <a
class="moz-txt-link-freetext"
href="https://www.postgresql.org/docs/current/ddl-generated-columns.html"
moz-do-not-send="true">https://www.postgresql.org/docs/current/ddl-generated-columns.html</a></p>
<p>They are conceptually similar between QGIS and PostgreSQL, but
they are not propagated from QGIS to the data source as a new
column there. They live only in the QGIS project.</p>
<p>If you want a generated column in your PostgreSQL database, you
need to add it another way. E.g. via an SQL query.</p>
<p>Cheers, Hannes</p>
<div class="moz-cite-prefix">On 26.07.24 12:46, Roland Berger via
QGIS-User wrote:<br>
</div>
<blockquote type="cite"
cite="mid:7738df1f-a73c-4928-9438-f47b273cb013@steinerpartner.com">
<meta http-equiv="content-type"
content="text/html; charset=UTF-8">
Hi <br>
<br>
I would like to add a virtual field to a PostGIS vector layer
with PyQGIS with standalone scripts (<a
class="moz-txt-link-freetext"
href="https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/intro.html#python-applications"
moz-do-not-send="true">https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/intro.html#python-applications</a>)<br>
All examples I have found so far with google, chatgpt4 and
JetBrains AI Assistant gave me solutions that do not work. They
only work in the Python Console inside QGIS.<br>
Does anybody know of a solution that works with PyQGIS with
standalone scripts?<br>
See below what I have tried so far in different variations.<br>
<br>
Thanks for any help <br>
Roland<br>
<br>
Example created by chatgpt:<br>
...<br>
<pre>field_name = "virtual_field"</pre>
<pre>expression = '"attribute_name" * 2' # Example expression</pre>
<pre># Create and add the virtual field</pre>
<pre>layer.startEditing()</pre>
<pre>layer.addExpressionField(expression, QgsField(field_name, QVariant.Double))</pre>
<pre>layer.commitChanges()</pre>
...<br>
<br>
<br>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
QGIS-User mailing list
<a class="moz-txt-link-abbreviated moz-txt-link-freetext"
href="mailto:QGIS-User@lists.osgeo.org" moz-do-not-send="true">QGIS-User@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext"
href="https://lists.osgeo.org/mailman/listinfo/qgis-user"
moz-do-not-send="true">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"
moz-do-not-send="true">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
</blockquote>
<pre class="moz-signature" cols="72">--
Johannes Kröger / GIS-Entwickler/-Berater
***********************************************************
FOSS Academy Sommerschule: Kompaktkurs zum Aufbau einer GDI
02.-06. September 2024, Präsenzveranstaltung in Bonn
<a class="moz-txt-link-freetext"
href="https://www.foss-academy.com/kompaktkurse"
moz-do-not-send="true">https://www.foss-academy.com/kompaktkurse</a>
***********************************************************
WhereGroup GmbH
c/o KK03 GmbH
Lange Reihe 29
20099 Hamburg
Germany
Tel: +49 (0)228 / 90 90 38 - 36
Fax: +49 (0)228 / 90 90 38 - 11
<a class="moz-txt-link-abbreviated moz-txt-link-freetext"
href="mailto:johannes.kroeger@wheregroup.com"
moz-do-not-send="true">johannes.kroeger@wheregroup.com</a>
<a class="moz-txt-link-abbreviated" href="http://www.wheregroup.com"
moz-do-not-send="true">www.wheregroup.com</a>
Geschäftsführer:
Olaf Knopp, Peter Stamm
Amtsgericht Bonn, HRB 9885
-------------------------------
</pre>
<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>
<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>