Friday, 1 July 2016

pyqgis - How to get a group layer delete signal in QGIS



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?



Answer



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.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...