2

I'm developing a plugin into QGIS 2.18. I need to catch if the user deletes a group layer. I tried the signal layerWillBeRemoved from QgsMapLayerRegistry class but it sends only layers not group layer.

What should I do?

1 Answer 1

2

You need check this signals onWillRemoveChildren and onRemovedChildren, check the Api documentation

This is a sample code for removed signals nodes:

root = QgsProject.instance().layerTreeRoot()

def onWillRemoveChildren(node, indexFrom, indexTo):
  print "Will Remove Node : ",node, indexFrom, indexTo

def onRemovedChildren(node, indexFrom, indexTo):
  print "Remove Node : ", node, indexFrom, indexTo

root.willRemoveChildren.connect(onWillRemoveChildren)
root.removedChildren.connect(onRemovedChildren)

mygroup = root.findGroup("test_group")
root.removeChildNode(mygroup)

You can expand this function for show a custom message,check if the index is a specific group or something like that.

1
  • Sorry for the delay but this signal caused a crash dump in QGIS if a layer is deleted (but it's ok for a group layer).
    – SYG
    Feb 3, 2018 at 17:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.