2

In a QgsComposition i would like to retrieve the list of all Composer Items.

I don't want to specify item type.

The function should have been composerItems() but the help says it isn't available in Python Bindings (I'm using QGIS 2.18).

Is there a way to access that list ?

2 Answers 2

6

You can just call "items()":

the_items = my_composer.items()
3
  • That answer should be the accepted answer. It is correct and the shortest. Apr 3, 2018 at 10:58
  • Thanks Nyall ... i'm so sad i'm not able to find it by myself in the online class reference documentation ...
    – Snaileater
    Apr 3, 2018 at 19:05
  • That's because it comes from the underlying QGraphicsScene base class - there's a link on the doc page to see the base class members
    – ndawson
    Apr 4, 2018 at 6:39
3

You can use following structure: (as an example)

composition = iface.activeComposers()[0].composition()
items_model = composition.itemsModel()
list_of_composer_items = items_model.zOrderList()

# OUT:
# [<qgis._core.QgsComposerArrow object at ..>, <qgis._core.QgsComposerLegend 
# object at ..>, <qgis._core.QgsComposerScaleBar object at ..>, 
# <qgis._core.QgsComposerMap object at ..>]

NOTE: "This list includes both items currently in the composition and items which have been removed from the composition."

UPDATE: Thanks to @ndawson. composition.items() returns existing items in a composition in addition to three more objects; two QGraphicsRectItem and a QgsPaperItem (QGIS 2.18). Even if you don't add any item, those three items will exist in the list.

>>> composition = iface.activeComposers()[0].composition()
>>> composition.items()    
[<PyQt4.QtGui.QGraphicsRectItem object at ..>,
 <PyQt4.QtGui.QGraphicsRectItem object at ..>,
 <qgis._core.QgsPaperItem object at ..>]
3
  • Thanks Kadir ... i didn't have a look at this QgsComposerModel so far ...
    – Snaileater
    Apr 2, 2018 at 17:34
  • If you reopen QGIS, you just get existing items. In addition to existing items, using zOrderList() gives you items removed in related session. Apr 2, 2018 at 17:45
  • Is isRemoved the function to use to discriminate those removed and the other ? Haven't tested it yet ...
    – Snaileater
    Apr 2, 2018 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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