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