3

I have created 3 print layouts in QGIS3. Now I am trying to get a list of all layouts in the LayoutManager with python. This is how I am trying to do it(this code is executed in the python console of QGIS3):

>>> from qgis.core import QgsLayoutManager
>>> layoutmanager = QgsLayoutManager()
>>> layoutmanager.layouts()
>>> []

What I get is an empty list. So what am I missing? Do I have to save the Layouts in a special way? For those who don't understand what I mean with layout, in QGIS3 Layout is the new Composer.

1 Answer 1

5
+50

With your code, you are creating a new instance of QgsLayoutManager. So, you don't get current list but an empty one.

You should retrieve the project instance with

projectInstance = QgsProject.instance()

From it, you can get the current layoutManager instance and deduce the layouts

projectLayoutManager = projectInstance.layoutManager()

projectLayoutManager.layouts()

If you are not aware of it, the new Python API docs really helped https://qgis.org/pyqgis/master/core/QgsProject.html

2
  • Yeah thanks, I wasn't aware that there is a new Python API.
    – Blinxen
    Commented Jan 22, 2018 at 10:38
  • In fact, it's the docs for Python API who is new not the Python API itself (only some changes compared to QGIS 2.x version)
    – ThomasG77
    Commented Jan 22, 2018 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.