[Qgis-developer] Working with the new Legend

Richard Duivenvoorde rdmailings at duif.net
Sat Jul 5 06:50:54 PDT 2014


On 28-06-14 08:11, Martin Dobias wrote:
> Let me paste some example code. Let's prepare few things to begin with:
> 
> # add few layers at the beginning so the legend is not completely empty
> v1 = iface.addVectorLayer("Point", "Layer 1", "memory")
> v2 = iface.addVectorLayer("Point", "Layer 2", "memory")
> v3 = iface.addVectorLayer("Point", "Layer 3", "memory")
> 
> # create a layer and add it to registry, saying it should not be
> immediately added to legend
> v4 = QgsVectorLayer("Point", "Layer X", "memory")
> QgsMapLayerRegistry.instance().addMapLayer(v4, False)
> 
> # get reference to the layer tree
> root=QgsProject.instance().layerTreeRoot()
> 
> 
>> * insert a layer at a give location
> 
> # add layer to the node at index 0
> v4n = QgsLayerTreeLayer(v4)
> root.insertChildNode(0, v4n)
> 
> 
>> * move a layer to a new position
> 
> v4n2 = v4n.clone()
> root.insertChildNode(-1, v4n2) # insert at last position
> root.removeChildNode(v4n)
> 
> 
>> * remove a layer
> 
> root.removeChildNode(v4n2)
> 
> (we could use root.findLayer(v4.id()) to get the node if we didn't
> have it before)

Hi Martin,

if I want to insert a new layer below some other one, I need to know the
position of that one. I'm trying to find an index or so in Python, but
cannot find it?

Is there already a way to do this? (I want to put a buffer below the
layer it has buffered)

Regards,

Richard



More information about the Qgis-developer mailing list