I will preface this answer with the following:
When I run the first part of your code which creates and adds the temporary layers, they are added one above the other starting with canvas
so that "canv" is at the bottom, then the line layers and finally the point layer on top which is already in the logical order.
The problem is that the line layers are placed below each other.
This problem statement does not make any sense to me.
but the if condition is not satisfied because the ch.name is "temp",
the same as the other temporary layers added to this project.
I do not reproduce this described behavior.
However, here are some different options to reorder layer nodes in the layer tree view.
The simplest is instead of adding layers to the project one at a time, just use the addMapLayers()
method and pass a list of layers you want to add in the desired order starting with index 0 on top:
canvas = QgsVectorLayer("Polygon?crs=epsg:3003", "canv", "memory")
cartesiano = QgsVectorLayer("LineString?crs=epsg:3003", "temp", "memory")
struttura = QgsVectorLayer("LineString?crs=epsg:3003", "grafico", "memory")
layerAnni = QgsVectorLayer('Point?crs=epsg:3003', 'puntiAnni', 'memory')
layer_order = [layerAnni, struttura, cartesiano, canvas]
QgsProject.instance().addMapLayers(layer_order)
Result:
You could also use the reorderGroupLayers() method which was added in QGIS 3.30:
canvas = QgsVectorLayer("Polygon?crs=epsg:3003", "canv", "memory")
cartesiano = QgsVectorLayer("LineString?crs=epsg:3003", "temp", "memory")
struttura = QgsVectorLayer("LineString?crs=epsg:3003", "grafico", "memory")
layerAnni = QgsVectorLayer('Point?crs=epsg:3003', 'puntiAnni', 'memory')
start_order = [struttura, canvas, layerAnni, cartesiano]
QgsProject.instance().addMapLayers(start_order)
new_order = [layerAnni, struttura, cartesiano, canvas]
root = QgsProject.instance().layerTreeRoot()
root.reorderGroupLayers(new_order)
Result:
Another option is, when adding layer/layers to the project via addMapLayer()
or addMapLayers()
, pass False
as the second argument which will add the layers to the project but not the layer tree/legend, then you can insert the layers to the root group in the order you want:
canvas = QgsVectorLayer("Polygon?crs=epsg:3003", "canv", "memory")
cartesiano = QgsVectorLayer("LineString?crs=epsg:3003", "temp", "memory")
struttura = QgsVectorLayer("LineString?crs=epsg:3003", "grafico", "memory")
layerAnni = QgsVectorLayer('Point?crs=epsg:3003', 'puntiAnni', 'memory')
map_layers = [struttura, canvas, layerAnni, cartesiano]
QgsProject.instance().addMapLayers(map_layers, False)
root = QgsProject.instance().layerTreeRoot()
new_order = [layerAnni, struttura, cartesiano, canvas]
for i, lyr in enumerate(new_order):
root.insertLayer(i, lyr)
Result:
Finally, if you want to want to create a new layer node and remove the old one, you need to clone the original node, otherwise QGIS will crash. See the docs for insertChildNode() which state that:
The node must not have a parent yet. The node will be owned by this
group.
canvas = QgsVectorLayer("Polygon?crs=epsg:3003", "canv", "memory")
cartesiano = QgsVectorLayer("LineString?crs=epsg:3003", "temp", "memory")
struttura = QgsVectorLayer("LineString?crs=epsg:3003", "grafico", "memory")
layerAnni = QgsVectorLayer('Point?crs=epsg:3003', 'puntiAnni', 'memory')
start_order = [layerAnni, canvas, struttura, cartesiano]
QgsProject.instance().addMapLayers(start_order)
root = QgsProject.instance().layerTreeRoot()
for ch in root.children():
print(ch)
if ch.name() == 'canv':
root.insertChildNode(len(root.children()), ch.clone())
root.removeChildNode(ch)
Result: