I wrote this code 5 years ago because I wanted any layer added to the layer panel to be on top not the bottom.
def add_layer_to_map(self, layer: QgsMapLayer): """Add layer to top of layer window because QGIS always puts at the bottom in layer order panel. Args: layer (QgsMapLayer): Base class for all map layer types. """ QgsProject.instance().addMapLayer(layer) root = QgsProject.instance().layerTreeRoot() root.setHasCustomLayerOrder(True) order = root.customLayerOrder() order.insert(0, order.pop(order.index(layer))) root.setCustomLayerOrder(order) layer.triggerRepaint() root.setHasCustomLayerOrder(False) From: QGIS-Developer <qgis-developer-boun...@lists.osgeo.org> On Behalf Of coder via QGIS-Developer Sent: Wednesday, October 23, 2024 11:05 AM To: qgis-developer@lists.osgeo.org Subject: [QGIS-Developer] How to turn on CustomLayerOrder (PyQGIS) Hello I'm trying to change the rendering order for the layers in the canvas with the aid of QgsLayerTree. The code grabs each layer, checks whether is a QgsMapLayer instance, and generates a reordered list (layersII). Then, the code executes: QgsLayerTree().setCustomLayerOrder(layersII) but nothing happens and print (QgsLayerTree().hasCustomLayerOrder()) returns False. The API documentation (Blockedhttps://qgis.org/pyqgis/master/core/QgsLayerTree.htmlBlocked) indicates that customLayerOrder will only be used when hasCustomLayerOrder is True. I'm missing something here but after browsing and googling, I just haven't figured out how to make the change in rendering order active. Thanks.
_______________________________________________ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer