I'm writing Python script in QGIS 2.18 to alert the user when multiple layers with different projections are added to the canvas. My codes are shown below.
from qgis.gui import QgsMessageBar input_layers =  layers = iface.legendInterface().layers() for layer in layers: if layer.type() == QgsMapLayer.RasterLayer: input_layers.append(layer) pcs_list =  for input in input_layers: pcs_list.append(input) pcs_str = list(set([pcs.crs().authid() for pcs in pcs_list])) text = "You have multiple layers with different CRS in this project: " + str(pcs_str).translate(None, "u") if len(pcs_str) >1: iface.messageBar().pushMessage("Alert",text, level=QgsMessageBar.WARNING,duration=20) else: pass
This works fine when I run the script via the Python console. However I'd like this alert to pop-up automatically when new layers (with different projections) are added to the layers panel. I'm aware that layer objects in the layers panel can be accessed through the the layer tree root. Is there any method to monitor changes in the layer tree root?