<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
mso-ligatures:none;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">I wrote this code 5 years ago because I wanted any layer added to the layer panel to be on top not the bottom.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">def add_layer_to_map(self, layer: QgsMapLayer):<o:p></o:p></p>
<p class="MsoNormal"> """Add layer to top of layer window because QGIS<o:p></o:p></p>
<p class="MsoNormal"> always puts at the bottom in layer order panel.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> Args:<o:p></o:p></p>
<p class="MsoNormal"> layer (QgsMapLayer): Base class for all map layer types.<o:p></o:p></p>
<p class="MsoNormal"> """<o:p></o:p></p>
<p class="MsoNormal"> QgsProject.instance().addMapLayer(layer)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> root = QgsProject.instance().layerTreeRoot()<o:p></o:p></p>
<p class="MsoNormal"> root.setHasCustomLayerOrder(True)<o:p></o:p></p>
<p class="MsoNormal"> order = root.customLayerOrder()<o:p></o:p></p>
<p class="MsoNormal"> order.insert(0, order.pop(order.index(layer)))<o:p></o:p></p>
<p class="MsoNormal"> root.setCustomLayerOrder(order)<o:p></o:p></p>
<p class="MsoNormal"> layer.triggerRepaint()<o:p></o:p></p>
<p class="MsoNormal"> root.setHasCustomLayerOrder(False)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> QGIS-Developer <qgis-developer-bounces@lists.osgeo.org>
<b>On Behalf Of </b>coder via QGIS-Developer<br>
<b>Sent:</b> Wednesday, October 23, 2024 11:05 AM<br>
<b>To:</b> qgis-developer@lists.osgeo.org<br>
<b>Subject:</b> [QGIS-Developer] How to turn on CustomLayerOrder (PyQGIS)<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div id="compose-body-wrapper">
<div>
<div>
<p class="MsoNormal">Hello<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> QgsLayerTree().setCustomLayerOrder(layersII)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">but nothing happens and<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> print (QgsLayerTree().hasCustomLayerOrder()) <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>