[QGIS-Developer] Difference in layer order between map and exported image

Johannes Kröger (WhereGroup) johannes.kroeger at wheregroup.com
Wed May 28 03:50:11 PDT 2025


Hi!

This is a stab in the dark but I hope it will at least point you in the 
right direction. :)

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.

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").

In this case *maybe* 
https://qgis.org/pyqgis/master/core/QgsLayerTree.html#qgis.core.QgsLayerTree.customLayerOrderChanged 
is the right signal to use?

The logic would be e.g.:

    def my_export_function():
         ...
         my_project = QgsProject.instance()
         ...
         exporter.exportToImage(png_name, settings=canvas_settings)

    ...

    my_tree.customLayerOrderChanged.connect(my_export_function)
    # ^ no parentheses here, you pass the function object! ^

    my_tree.setCustomLayerOrder(my_layers)
    my_tree.setHasCustomLayerOrder(True)
    ...

Now the my_export_function function would be called when the 
QgsLayerTree says that its custom layer order has been changed.

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.

Cheers, Hannes

On 5/25/25 19:05, AFernandez via QGIS-Developer wrote:
> Hello,
> I'm reordering the map layers using
>     my_instance = QgsProject.instance()
>     my_tree = my_instance.layerTreeRoot()
>      ...
>     my_tree.setCustomLayerOrder(my_layers)
>     my_tree.setHasCustomLayerOrder(True)
> 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:
> def my_export_function(...):
>     my_project = QgsProject.instance()
>     manager = my_project.layoutManager()
>     layout = QgsPrintLayout(my_project)
>     layout.initializeDefaults()
>     ... (lines to set up canvas size and others)
>     exporter = QgsLayoutExporter(layout)
>     canvas_settings = exporter.ImageExportSettings()
>     exporter.exportToImage(png_name, settings=canvas_settings)
> 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.
> Thanks.
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-- 
Johannes Kröger / GIS-Entwickler/-Berater

****************************************
WhereGroup Shorts 2025 - am 15. Mai
Im Fokus: PostgreSQL & PostGIS
Online als Zoom-Meeting
https://wheregroup-shorts.de
****************************************

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

johannes.kroeger at wheregroup.com
www.wheregroup.com

Geschäftsführer:
Olaf Knopp, Peter Stamm
Amtsgericht Bonn, HRB 9885
-------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20250528/72da5abe/attachment.htm>


More information about the QGIS-Developer mailing list