[QGIS-Developer] QGIS Server accessing QgsProject

Ricardo Filipe Soares Garcia da ricardo.garcia.silva at gmail.com
Mon Mar 31 13:05:32 PDT 2025


Hi all

I am implementing a custom plugin for QGIS Server that uses a
QgsServerFilter subclass.

The purpose of this plugin is to use QGIS Server to respond to `GetPrint`
requests. However, I need to pass some runtime parameters to my map layout
before handing it over to `GetPrint`.

I think the place where I should do these preparatory steps, which
ultimately include loading some layers and setting some layout variables,
is in my `onRequestReady()` method.

The problem: I need to be able to set properties on the relevant
QgsPrintLayout instance, but in order to do that I would need to retrieve
it from the current QgsProject instance, and I can't figure out how to get
a hold of it.

Is it possible to get the current instance of QgsProject in the context of
QGIS server?

At first I tried to do it as I would on a plugin for the desktop:

```
qgis_project = QgsProject.instance()
layout_manager = qgis_project.layoutManager()
my_layout = layout_manager.getLayoutByName("the_name")
# this fails, my_layout is None
```

This approach does not work, as the resulting qgis_project is not the one I
am using in my request.

Then I searched through the properties of `QgsServerInterface`, hoping to
find some sort of `getCurrentProject()` method - but this does not seem to
exist.

I also tried loading the project from disk in my plugin with

```
current_project = QgsProject.instance()
current_project.read(server_iface..configFilePath())
```

This second approach does allow me to load up the project, but any changes
I set at runtime are not propagated to subsequent handlers in the QGIS
Server workflow, meaning that the `GetPrint` handler does not see my
changes to the project.

I don't want to save the changes, just set them at runtime and use them.
This is because other (potentially parallel) requests will also need to
modify the aforementioned layout in order to customize their printing needs.

Is it at all possible to do what I am trying to do? Or is there maybe a
more proper way?

Thanks in advance

-- 
___________________________ ___ __
Ricardo Garcia Silva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20250331/6b6ac167/attachment.htm>


More information about the QGIS-Developer mailing list