[QGIS-Developer] How to turn on CustomLayerOrder (PyQGIS)
Catania, Luke A ERDC-RDE-GRL-VA CIV
Luke.A.Catania at erdc.dren.mil
Wed Oct 23 14:44:04 PDT 2024
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-bounces at lists.osgeo.org> On Behalf Of coder via QGIS-Developer
Sent: Wednesday, October 23, 2024 11:05 AM
To: qgis-developer at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20241023/dfb993d8/attachment.htm>
More information about the QGIS-Developer
mailing list