4

I tried :

QgsProject.instance().layerTreeRoot().willRemoveChildren.connect(self.onWillRemoveChildren)

It works if I delete a group within in the root node. But if I delete a layer within a group QGIS crashes (core dump).

my code :

class Project:
    def __init__(self):
        self.name = ""
        self.layer_tree_group = self.set_layer_tree_group()
        self.layers = []


    def set_layer_tree_group(self):
        self.layer_tree_group = QgsLayerTreeGroup()
        self.layer_tree_group.setName(self.name)
        self.layer_tree_group.setCustomProperty("Project", self.project.name )


    def add_layer(self):
        point_layer = QgsVectorLayer("Point?crs=4326", "project_layer", "memory")
        QgsMapLayerRegistry.instance().addMapLayer(point_layer, False)
        self.layer_tree_group.insertLayer(0, layer)


 class PluginInterface:

    list_project = []

    def __init__(self): 
        self.active_project = self.group_selected
        QgsProject.instance().layerTreeRoot().willRemoveChildren.connect(self.onWillRemoveChildren)

    def onWillRemoveChildren(self, node, indexFrom, indexTo):
        print "Will remove", node.name(), indexFrom, indexTo

The aim is to delete instance of Project if a user remove a group or a layer in the legend.

How can achieve it ?

  • mm...this code not work? gis.stackexchange.com/a/264888/49538, I'm testing it and it works well – Fran Raga Feb 12 '18 at 14:00
  • Are you sure that disconnect the signal,and only connect one time? – Fran Raga Feb 12 '18 at 14:02
  • I suppose you've got a point, I'm working on it... But it's difficult to debug because of QGis crash dump. – SYG Feb 14 '18 at 7:51
  • I'm still getting error "std::bad_alloc" when I delete a layer within a group, and I'm pretty sure that I connect only one time my signal and disconnect it on unload method. – SYG Feb 14 '18 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.