I have almost finished to implement the following idea:

When moving a raster layer to a distinct group load a style from a QML style file.

I'm using this code in the Python console in QGIS:

def isInstanceOfGroup():
    root = QgsProject.instance().layerTreeRoot()
    subchild_list = []
    for child in root.children():
        for subchild in child.children():
            if isinstance(subchild, QgsLayerTreeLayer):
                if subchild.parent().name() == 'MyGroup':

    layer = iface.activeLayer()    
    if iface.activeLayer().name() in subchild_list:


Two things that do not work properly:

  1. When I move a layer to the group it don't sets the style when I release the layer (left mouse button). It sets the style when I click on more time on layer.

  2. After the layer is clicked the style is set but the legend has not changed. It changed until I move the layer within the group one more time.

What do I have to change that after releasing/dropping the layer to the group style/legend are changed immediately?

  • yes is normal currentLayerChanged is a signal for detect if the click layer change,not for detect TOC movement – Fran Raga Sep 4 '18 at 11:50
  • @FranciscoRaga - Is there a signal for detecting TOC movement? – Stefan Sep 4 '18 at 12:10
  • I think not,but I'm not sure – Fran Raga Sep 4 '18 at 12:38

Your Answer

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

Browse other questions tagged or ask your own question.