0

The main goal is to remove this error:

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

It appears when a user (I'm in the testing phase) deletes a QGIS layer from the layer list and leaves a plug-in enabled that still stores data from the layers even though it has been deleted.

ptpla = QgsProject.instance().mapLayersByName('OT_PTPL_A') # the layer is listed in the QGIS Program 
if ptpla:
        ptpl = ptpla[0]
        unique_idensimc = set()
        unique_idenulic = set()
        if ptpl and ptpl.isValid() and self.modelSimcPtpl.rowCount() < 1: #here appers the problem
        # Get attributes from the layer and add them to the model. It only loads once at the beginning
            for feature in ptpl.getFeatures():
                idenulic = str(feature.attributes()[ptpl.fields().indexFromName('FEATUREID')])
                 #...
            self.dlg.pushButton_47.clicked.connect(self.markPtpl) # this line was initially outside the If, ​​but it did not solve the problem

The error appears with the ptpla variable - if the layer it downloaded has been deleted, the variable becomes empty.

I tried with else conditions (example in the previous post), but do has no effect.

This error also appears in the "markPtpl" function (2nd independent place) The function is triggered with a button (pushButton) that retrieves data from the comboBox entered above. More details below

''' ptpl = ptpla[0] simcPtpl = self.dlg.comboBox_29.currentText() ulicPtpl = self.dlg.comboBox_30.currentText() expression= f'IDENTYFIKATORSIMC= {simcPtpl} AND IDENTYFIKATORULIC = {ulicPtpl}' #after removing the layer from QGIS's layer list, comboBoxes still store data, but do not download to the query

ptpl.selectByExpression(expression) #error after removing a layer from the QGIS's layer list crashes here, but adding else for this condition here does not solve the problem
dataPtpl = []
if self.model8.rowCount() > 0: #...

I'd tried with else statements and pass, but both doesn't solve it.

Have your different idea to solve it?

2
  • As it is currently written, your question will attract opinion-based answers which are discouraged on this site. Have you thought about how you want to handle the situation. For example, do you want to prevent the user removing the layer? (In which case you might consider protecting it). Or, would you prefer the plugin to recognise when the layer is removed and not attempt the code in your question?
    – Matt
    Commented Jan 25 at 13:36
  • I prefer second option. About first I need to ask my boss Commented Jan 25 at 14:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.