2

I try to use this code from QGIS 2.x version in QGIS 3.0:

li = iface.legendInterface()
li.moveLayer(demLayer, self.watershedGroupIndex

I found QgsLayerTreeLayer but can not use it. I want to move existing layer on layers panel by using gruop index, because I have layer and group index. However examples show me, adding map layer like that:

QgsProject.instance().addMapLayer(demLayer, False)
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup(index.name())
group.insertChildNode(0, QgsLayerTreeLayer(myClone))

But I already loaded layer just want to move it to group by using its index.

  • What does 'index' mean in your question? Group layer order or anything else? – Kadir Şahbaz Apr 15 '18 at 15:55
2

findGroup finds group node by its name not by index. You already know index, so, you get group layer by root.children()[index].

root = QgsProject.instance().layerTreeRoot()

myClone= demLayer.clone()
group = root.children()[index] # index: group layer index
group.insertChildNode(0, QgsLayerTreeLayer(myClone))

# EDIT:
# after that, two layers are different although they have same source and style
demLayer == myClone  # is False


root.removeLayer(demLayer)

For more information: QGIS Layer Tree API (Part 2)

Moving a node(group or layer) is done in three steps: 1. clone the existing node, 2. add the cloned node to the desired place in layer tree, 3. remove the original node.

  • My layer has already uploaded I just want to move it to group. Your suggestion is adding map by defining false. However I have already loaded my layer – Mustafa Uçar Apr 15 '18 at 13:00
  • What I understand is that you have demLayer and a grup layer that you know its index, and you want to move demLayer to that group layer. Right? – Kadir Şahbaz Apr 15 '18 at 13:56
  • Yes but this dem layer is not in folder or etc. It is uploaded already. I do not need to give path. – Mustafa Uçar Apr 15 '18 at 16:31
  • OK. I editted the answer. I guess, there is no way of moving a layer directly in QGIS3. – Kadir Şahbaz Apr 15 '18 at 16:50
  • 1
    @GISme It works in QGIS 3, too. – Kadir Şahbaz Jul 19 at 14:02

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.