<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Dear QGIS Developer Community, <br>
</p>
<p>I like to use a QgsExpressionContextGenerator to create the
expression context for a QgsExpressionLineEdit.<br>
However, when executed in the QGIS python shell, the following
code crashes my QGIS after clicking on the Expression builder
widget:</p>
<div class="highlight highlight-source-python">
<pre><span class="pl-k">from</span> <span class="pl-s1">qgis</span>.<span class="pl-s1">core</span> <span class="pl-k">import</span> (
<span class="pl-v">QgsExpressionContextGenerator</span>, <span class="pl-v">QgsExpressionContext</span>, <span class="pl-v">QgsExpressionContextUtils</span>
)
<span class="pl-k">from</span> <span class="pl-s1">qgis</span>.<span class="pl-s1">gui</span> <span class="pl-k">import</span> <span class="pl-v">QgsExpressionLineEdit</span>
<span class="pl-k">class</span> <span class="pl-v">Generator</span>(<span class="pl-v">QgsExpressionContextGenerator</span>):
<span class="pl-k">def</span> <span class="pl-en">__init__</span>(<span class="pl-s1">self</span>, <span class="pl-c1">*</span><span class="pl-s1">args</span>, <span class="pl-c1">**</span><span class="pl-s1">kwds</span>):
<span class="pl-en">super</span>(<span class="pl-v">Generator</span>, <span class="pl-s1">self</span>).<span class="pl-en">__init__</span>(<span class="pl-c1">*</span><span class="pl-s1">args</span>, <span class="pl-c1">**</span><span class="pl-s1">kwds</span>)
<span class="pl-k">def</span> <span class="pl-en">createExpressionContext</span>(<span class="pl-s1">self</span>) <span class="pl-c1">-></span> <span class="pl-v">QgsExpressionContext</span>:
<span class="pl-s1">context</span> <span class="pl-c1">=</span> <span class="pl-v">QgsExpressionContext</span>([<span class="pl-v">QgsExpressionContextUtils</span>.<span class="pl-en">globalScope</span>()])
<span class="pl-en">print</span>(<span class="pl-s">f'Generated QgsExpressionContext: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">context</span><span class="pl-kos">}</span></span>'</span>, <span class="pl-s1">flush</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-k">return</span> <span class="pl-s1">context</span>
<span class="pl-s1">gen</span> <span class="pl-c1">=</span> <span class="pl-v">Generator</span>()
<span class="pl-s1">w</span> <span class="pl-c1">=</span> <span class="pl-v">QgsExpressionLineEdit</span>()
<span class="pl-s1">w</span>.<span class="pl-en">setExpression</span>(<span class="pl-s">"'click edit button ->'"</span>)
<span class="pl-s1">w</span>.<span class="pl-en">registerExpressionContextGenerator</span>(<span class="pl-s1">gen</span>)
<span class="pl-s1">w</span>.<span class="pl-en">show</span>()
<span class="pl-c"># now click on edit button to produce a crash</span></pre>
</div>
<pre class="moz-signature" cols="72">
<img src="https://user-images.githubusercontent.com/1404870/99965039-913c7400-2d94-11eb-9def-5d3c4024d433.png" alt="grafik" style="max-width:100%;">
</pre>
<p>Any suggestions how I can fix this?</p>
<p>Thanks, Benjamin</p>
<pre class="moz-signature" cols="72">
</pre>
<pre class="moz-signature" cols="72">--
Benjamin Jakimow, Doctoral Researcher
Earth Observation Lab | Geography Department | Humboldt-Universität zu Berlin
e-mail: <a class="moz-txt-link-abbreviated" href="mailto:benjamin.jakimow@geo.hu-berlin.de">benjamin.jakimow@geo.hu-berlin.de</a>
phone: +49 (0) 30 2093 6846
mobile: +49 (0) 157 5656 8477
fax: +49 (0) 30 2093 6848
mail: Unter den Linden 6 | 10099 Berlin | Germany
room: 2'211
web: <a class="moz-txt-link-freetext" href="https://hu-berlin.de/eo-lab">https://hu-berlin.de/eo-lab</a></pre>
</body>
</html>