<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'>
<p>Hi Ale,</p>
<p>Thanks for the hint!</p>
<p>With</p>
<p>expContext.setFeature(form.feature())</p>
<p>it seems to work.</p>
<p>I was assuming that adding the formScope would also add the feature context ... but apparently not.</p>
<p>Now I can continue adding the more complex expression.</p>
<p>Thank you for your help!</p>
<p>Andreas</p>
<p id="reply-intro">On 2022-11-07 11:46, Alessandro Pasotti wrote:</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">
<div class="pre" style="margin: 0; padding: 0; font-family: monospace">You need to set the feature if you want to access the feature fields.<br /><br />QgsExpressionContext::setFeature( const QgsFeature &feature )<br /><br />If you want the form feature values you need to use the current_value<br />or the current_feature functions.<br /><br />Regards.<br /><br />On Mon, Nov 7, 2022 at 11:27 AM Andreas Neumann via QGIS-Developer<br /><<a href="mailto:qgis-developer@lists.osgeo.org">qgis-developer@lists.osgeo.org</a>> wrote:
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0"><br />Hi,<br /><br />I am trying to implement a Python action in "form" and "feature" action scope that uses a QGIS expression, but fail to get a valid expression calculation.<br /><br />Here is my code so far:<br /><br />from qgis.PyQt.QtWidgets import *<br />from qgis.core import QgsMessageLog<br /><br />expContext = QgsExpressionContext()<br />expContext.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(form.layer()))<br />expContext.appendScope(QgsExpressionContextUtils.formScope(form.feature(),'SingleEditMode'))<br /><br />exp = QgsExpression('t_id')<br /><br />expResult = exp.evaluate(expContext)<br />QgsMessageLog.logMessage('Expression Result=' + str(expResult))<br /><br /><br />So even the most basic expression (getting a field value) will not work in my code. I always get "None" as a result.<br /><br />Any idea what is missing? Am I missing a certain scope or an import statement?<br /><br />Thank you very much for any hints what might be wrong here ...<br /><br />Andreas<br /><br />_______________________________________________<br />QGIS-Developer mailing list<br /><a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a><br />List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank" rel="noopener noreferrer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br />Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank" rel="noopener noreferrer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a></blockquote>
<br /><br /></div>
</blockquote>
<p><br /></p>

</body></html>