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:
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.
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?