Hi Luke,

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. 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.

In that case your self.aoi_layer python reference still exists, but not the underlying C++ object.

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). Or save the aoi_layer_id and test in your eventFilter, if the aoi_layer instance still exists:

aoi_layer = self.project().mapLayers().get(self.aoi_layer_id)

if isinstance(aoi_layer, QgsMapLayer):
   aoi_layer_data_provider = aoi_layer.dataProvider()

  ...


Greetings,

Benjamin

Am 04.05.2023 um 04:46 schrieb Catania, Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer:

I am trying to understand the general meaning of the message below.

RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted

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.

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:

    def eventFilter(self, source, event):

          if event.type() == QEvent.MouseButtonPress and event.button() == Qt.RightButton:

                        if self.valid_size(self.aoi_area):

aoi_layer_data_provider = self.aoi_layer.dataProvider()

self.aoi_feature.setGeometry(self.aoi_geometry)

aoi_layer_data_provider.addFeatures([self.aoi_feature])

self.aoi_layer.commitChanges()

self.aoi_layer.setExtent(self.aoi_feature.geometry().boundingBox())

self.add_area_perimeter()

self._refresh_and_zoom_to_layer(True)

self.AOI_SIZE_VALIDATOR_LABEL.close()

self.track_cursor = False

iface.mapCanvas().viewport().removeEventFilter(self)

    Return False

I initially had the return to be “return super().eventFilter(source, event)”

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.

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.

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.

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.


_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info:https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-developer

--
Dr. Benjamin Jakimow
Earth Observation Lab | Geography Department | Humboldt-Universität zu Berlin

e-mail:benjamin.jaki...@geo.hu-berlin.de
web:https://hu-berlin.de/eo-lab
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:https://www.linkedin.com/in/benjamin-jakimow
matrix: @jakimowb:hu-berlin.de

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
  • Re: [QGIS-Developer... Benjamin Jakimow via QGIS-Developer

Reply via email to