<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi Luke,</p>
<p>to my experience the "RuntimeError: wrapped C/C++ object of type
QgsVectorLayer has been deleted" is raised when the C++
QgsVectorLayer has been deleted by a parent class. <br>
This is often a QgsMapLayerStore, for example the
QgsProject.instance().mapLayerStore(), from which a layer gets
removed when it is removed from the layer tree.<br>
</p>
<p>In that case your self.aoi_layer python reference still exists,
but not the underlying C++ object.</p>
<p>You could ensure that the aoi_layer is not removed from the layer
store it is stored in, for example using
QgsLayerStore::takeMapLayer(layer) instead
QgsLayerStore::removeMapLayer(layer).<br>
Or save the aoi_layer_id and test in your eventFilter, if the
aoi_layer instance still exists:</p>
<p>aoi_layer = self.project().mapLayers().get(self.aoi_layer_id)<br>
</p>
<p>if isinstance(aoi_layer, QgsMapLayer):<br>
aoi_layer_data_provider = aoi_layer.dataProvider()</p>
<p> ...</p>
<p><br>
</p>
<p>Greetings,</p>
<p>Benjamin<br>
</p>
<div class="moz-cite-prefix">Am 04.05.2023 um 04:46 schrieb Catania,
Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer:<br>
</div>
<blockquote type="cite"
cite="mid:a116a2ef643d475eba5e19c4e1028025@erdc.dren.mil">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style>@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}div.WordSection1
{page:WordSection1;}</style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal">I am trying to understand the general
meaning of the message below.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">RuntimeError: wrapped C/C++ object of type
QgsVectorLayer has been deleted<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I step through my code in the debugger and
at some point my variable, aoi_layer, is longer assigned to a
specific vector layer, though the value comes back as
QgsVectorLayer since I have a watch on it. I though maybe
somehow it got set to “None”, but it’s not None.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’d like to post code, but I’d have way to
much code to post. I am basically trying to override the map
canvas events with an eventFIlter while I go through a drawing
process. I want a particular action to occur when I right
click on the map canvas. I am trying to override the map
canvas from completing a drawing of a shape if the shape is
too small, but it does not seem to override it. The shape is
completed when I right click, so it seems like I have not
overridden the event even though I capture the event using the
code below:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> def eventFilter(self, source, event):<o:p></o:p></p>
<p class="MsoNormal"> if event.type() ==
QEvent.MouseButtonPress and event.button() == Qt.RightButton:<o:p></o:p></p>
<p class="MsoNormal"> if
self.valid_size(self.aoi_area):<o:p></o:p></p>
<p class="MsoNormal">
aoi_layer_data_provider = self.aoi_layer.dataProvider()<o:p></o:p></p>
<p class="MsoNormal">
self.aoi_feature.setGeometry(self.aoi_geometry)<o:p></o:p></p>
<p class="MsoNormal">
aoi_layer_data_provider.addFeatures([self.aoi_feature])<o:p></o:p></p>
<p class="MsoNormal">
self.aoi_layer.commitChanges()<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">
self.aoi_layer.setExtent(self.aoi_feature.geometry().boundingBox())<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">
self.add_area_perimeter()<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">
self._refresh_and_zoom_to_layer(True)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">
self.AOI_SIZE_VALIDATOR_LABEL.close()<o:p></o:p></p>
<p class="MsoNormal">
self.track_cursor = False<o:p></o:p></p>
<p class="MsoNormal">
iface.mapCanvas().viewport().removeEventFilter(self)<span
style="font-size:10.5pt;font-family:Consolas;color:#D4D4D4"><o:p></o:p></span></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal"> Return False<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I initially had the return to be “return
super().eventFilter(source, event)”<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So the shape is completed and it adds it to
the layer that is open for editing. My function allows the
drawing to continue because my check indicates the shape is
not large enough so the commit should not happen unless the
size is valid.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I can continue drawing but I right click on
the map canvas it completes the drawing a second time, so now
I have two rectangles, both with the same start position, but
one larger than the other. It is when I create a second layer
to draw that I get the runtime error.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So I have two problems. I don’t seem to be
completely overriding the build in event handler and QGIS is
still completing the drawing when I right click. And somehow
this may be contributing to the runtime error.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">If I can understand in general how my
object is deleted when I don’t have any statement that deletes
the object maybe I can figure it out the second issue.
<o:p></o:p></p>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
QGIS-Developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
</pre>
</blockquote>
<pre class="moz-signature" cols="72">--
Dr. Benjamin Jakimow
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>
web: <a class="moz-txt-link-freetext" href="https://hu-berlin.de/eo-lab">https://hu-berlin.de/eo-lab</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
linkedin: <a class="moz-txt-link-freetext" href="https://www.linkedin.com/in/benjamin-jakimow">https://www.linkedin.com/in/benjamin-jakimow</a>
matrix: @jakimowb:hu-berlin.de</pre>
</body>
</html>