<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi!</p>
<p>This is a stab in the dark but I hope it will at least point you
in the right direction. :)</p>
<p>Some function calls trigger things that will not be done and
finished right away, but are processing asynchronously in the
background. It sounds like this might be happening here. You are
requesting a change to the layer order but I guess(!) QGIS takes
its time to do so in the background. While it is doing that, you
export the map image and that happens immediately at whatever
stage the layer reordering is currently at.</p>
<p>For those occasions, usually there are signals that communicate
the changed state at a later point in time. Those signals can be
connected to functions to be executed (so-called "slots").</p>
<p>In this case *maybe*
<a class="moz-txt-link-freetext"
href="https://qgis.org/pyqgis/master/core/QgsLayerTree.html#qgis.core.QgsLayerTree.customLayerOrderChanged">https://qgis.org/pyqgis/master/core/QgsLayerTree.html#qgis.core.QgsLayerTree.customLayerOrderChanged</a>
is the right signal to use?</p>
<p>The logic would be e.g.:</p>
<blockquote>
<p>def my_export_function():<br>
...<br>
my_project = QgsProject.instance()<br>
...<br>
exporter.exportToImage(png_name, settings=canvas_settings) <br>
<br>
...<br>
<br>
my_tree.customLayerOrderChanged.connect(my_export_function) <br>
# ^ no parentheses here, you pass the function object! ^<br>
<br>
my_tree.setCustomLayerOrder(my_layers)<br>
my_tree.setHasCustomLayerOrder(True)<br>
...</p>
</blockquote>
<p>Now the my_export_function function would be called when the
QgsLayerTree says that its custom layer order has been changed.<br>
</p>
<p>As I said, this might be a wrong guess or it might be a different
signal that you need to connect to, but maybe this gets you going
in the right direction.</p>
<p>Cheers, Hannes</p>
<div class="moz-cite-prefix">On 5/25/25 19:05, AFernandez via
QGIS-Developer wrote:<br>
</div>
<blockquote type="cite"
cite="mid:6a211ace5a6b06126f9af5985544e00a@odyhpc.com">Hello, <br>
I'm reordering the map layers using <br>
my_instance = QgsProject.instance() <br>
my_tree = my_instance.layerTreeRoot() <br>
... <br>
my_tree.setCustomLayerOrder(my_layers) <br>
my_tree.setHasCustomLayerOrder(True) <br>
This works smoothly and the map canvas looks as targeted. However,
the problem arises when calling a function to export the image.
The lines to accomplish this task read: <br>
def my_export_function(...): <br>
my_project = QgsProject.instance() <br>
manager = my_project.layoutManager() <br>
layout = QgsPrintLayout(my_project) <br>
layout.initializeDefaults() <br>
... (lines to set up canvas size and others) <br>
exporter = QgsLayoutExporter(layout) <br>
canvas_settings = exporter.ImageExportSettings() <br>
exporter.exportToImage(png_name, settings=canvas_settings) <br>
When the image is exported for the first time, It completely
ignores the order of the layers previously set up with
'my_layers'. The perplexing thing is that when the function is
called to export the image for a second time (without doing
anything else), the new exported image follows the new layer
order. I've been going over this issue for the last 2 weeks but
cannot figure out what controls the layer order of the exported
image or how to fix the issue. <br>
Thanks. <br>
_______________________________________________ <br>
QGIS-Developer mailing list <br>
<a class="moz-txt-link-abbreviated moz-txt-link-freetext"
href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a>
<br>
List info: <a class="moz-txt-link-freetext"
href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
<br>
Unsubscribe: <a class="moz-txt-link-freetext"
href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
<br>
<br>
</blockquote>
<pre class="moz-signature" cols="72">--
Johannes Kröger / GIS-Entwickler/-Berater
****************************************
WhereGroup Shorts 2025 - am 15. Mai
Im Fokus: PostgreSQL & PostGIS
Online als Zoom-Meeting
<a class="moz-txt-link-freetext" href="https://wheregroup-shorts.de">https://wheregroup-shorts.de</a>
****************************************
WhereGroup GmbH
c/o KK03 GmbH
Lange Reihe 29
20099 Hamburg
Germany
Tel: +49 (0)228 / 90 90 38 - 36
Fax: +49 (0)228 / 90 90 38 - 11
<a class="moz-txt-link-abbreviated moz-txt-link-freetext"
href="mailto:johannes.kroeger@wheregroup.com">johannes.kroeger@wheregroup.com</a>
<a class="moz-txt-link-abbreviated" href="http://www.wheregroup.com">www.wheregroup.com</a>
Geschäftsführer:
Olaf Knopp, Peter Stamm
Amtsgericht Bonn, HRB 9885
-------------------------------
</pre>
</body>
</html>