1

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':
                    subchild_list.append(subchild.layerName())

    layer = iface.activeLayer()    
    if iface.activeLayer().name() in subchild_list:
        layer.loadNamedStyle('c:/path_to_style_file/test.qml')
        layer.triggerRepaint()
    else:
        pass

iface.legendInterface().currentLayerChanged.connect(isInstanceOfGroup)

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?

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.