Monday, 8 January 2018

pyqgis - removeGroup() in QGIS 3


I am updating a plugin from QGIS 2 to 3. I need to iterate through the groups (the names are dynamic) and remove them with their respective child layers. I need to get a list of groups and then iterate through the groups and delete them. The original code is as follows:


legend = self.iface.legendInterface()
group_names = legend.groups()
rev = self.groups[:]
rev.reverse()

...
for i, group in enumerate(rev):
legend.removeGroup(group)

I replaced self.iface.legendInterface() with QgsProject.instance().layerTreeRoot() but group_names = legend.groups() fails with the message 'QgsLayerTree' object has no attribute 'groups'


What is the proper container of the groups and how do I access the collection; and, once found, how do I delete those groups? A search for information on how to remove groups from a layer tree in QGIS 3 has proven fruitless.




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