I am a novice to QGIS tasked with updating other developers' plugins from QGIS 2 to QGIS 3.

Here's the code I need to replace:

legend = self.iface.legendInterface()        
index = legend.addGroup('Quadrant Shapes', False)        
self.quad_group = index
mlr = QgsMapLayerRegistry.instance()

According to GitHub, iface.legendInterface() has been replaced with iface.layerTreeView(), so I can replace the first line with legend = self.iface.layerTreeView() but the next line fails because the QgsLayerTreeView object does not have an addGroup() attribute.

The general idea is to add a group to the interface and then append some layers to it.


With the input from Fran Raga and the discovery that moveLayer() is no longer available, the following solution worked for me:

root = QgsProject.instance().layerTreeRoot()
self.quad_group = root.addGroup('Quadrant Shapes')

self.north_layer = QgsVectorLayer([shp], "North_Quad", "ogr")
self.quad_group.insertChildNode(0, QgsLayerTreeLayer(self.north_layer))

self.south_layer = QgsVectorLayer(os.path.join([shp], "South_Quad", "ogr")
self.quad_group.insertChildNode(0, QgsLayerTreeLayer(self.south_layer))

For add new Group:

root = QgsProject.instance().layerTreeRoot()
group = root.addGroup("Quadrant Shapes")

And after add layer to this group:

layer =QgsVectorLayer(r"path_shp", "Layer Name", "ogr")
QgsProject.instance().addMapLayer(layer, False)
group.insertChildNode(0, QgsLayerTreeLayer(layer))
  • Thanks Fran. This gets me part of the way through the issue, but I am adding several layers to the group, and I will need to refer to those layers later in the code, so I will need to create variables to hold the layers, then add them collectively to the group. – CNIDog Apr 19 at 19:38
  • For example: self.north_layer = QgsVectorLayer([shp file], "North_Quad", "ogr") self.south_layer = QgsVectorLayer([shp file], "South_Quad", "ogr") ...etc. QgsProject.instance().addMapLayers([self.north_layer, self.south_layer, etc. Next I need to add those layers to the group. – CNIDog Apr 19 at 19:45
  • With moveLayers() gone, layers cannot be moved, only cloned and the original deleted, so the layer must be created within the destination group if it the layer is to be referred to later. Problem resolved with a modification to my code: self.north_layer = QgsVectorLayer(os.path.join([shp file], "North_Quad", "ogr") self.quad_group.insertChildNode(0, QgsLayerTreeLayer(self.north_layer)) self.south_layer = QgsVectorLayer([shp file]), "South_Quad", "ogr") self.quad_group.insertChildNode(0, QgsLayerTreeLayer(self.south_layer)) etc – CNIDog Apr 19 at 20:34
  • Update your question then – Fran Raga Apr 20 at 8:20
  • Update posted, @fran_raga. Thanks again for pointing me in the right direction. – CNIDog Apr 22 at 16:33

Your Answer

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

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