I developed a plugin for QGIS 3.x but I need to adjust it to work with QGIS 2.x too.
Here is code which is working nice with QGIS 3.x:
root = QgsProject.instance().layerTreeRoot() #QgsLayerTree
group_preview = root.findGroup("Preview")
if group_preview is not None:
root.removeChildNode(group_preview)
group = root.insertGroup(0, "Group")
raster_layer = QgsRasterLayer(urlWithParams, "new_layer", "wms")
QgsProject.instance().addMapLayer(raster_layer, False)
group.insertLayer(0, raster_layer)
Someone knows what I should use instead of :
root = QgsProject.instance().layerTreeRoot() #QgsLayerTree
to get QgsLayerTree() in QGIS 2.x?