Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I saved a template qpt for the map composer and now I want to read all the settings. I am able to insert the items with composition.addItemsfromXML... But this doesn't load the settings for the QgsPaperItem. This is the code:

composition = newCompView.composition()
file = QFile(self.plugin.rootDirectory + "ui/PrintTemplates/" + self.printTemplate)
doc = QDomDocument()
doc.setContent(file, False)
docElem = doc.documentElement()
paper = QgsPaperItem(composition)
paper.readXML(docElem, doc)
composition.addItemsFromXML( docElem, doc)

With readXML the paperItem remains empty. I just managed to set it manually in the code. But I want this to be loaded from the template...

share|improve this question
up vote 1 down vote accepted

ahoi, it would be good to know what version of QGIS you are using, and any other relevant particulars about your system.

According to the source for QgsComposition.readXML() the paper info is stored in the <Composition> tag of the saved template (at the bottom). QgsComposition.readXML reads the attributes of that tag and creates a new paper item. To get to that tag you can use:

compelm = dom.elementsByTagName("Composition").at(0).toElement()
composition.readXML(compelm, doc)

However, this did not work right for me (Mac OS X 1.7.3, QGIS 1.7.4), though it may for you. Furthermore, the QgsComposition.addItemsFromXML() method is commented out in the SIP file, so I can't even access it via Python on my setup, or in my nightly master branch builds.

There appears to be a disparity between the methods of the C++ source and the Python bindings at this time, because a look at the Composer's readXML() method now shows it also taking care of addItemsFrom XML().

If my solution works for you, I don't suggest sticking with it. It seems there must be a different way to approach this using the API.

Alternatively, you could also parse the <Composition> tag's attributes yourself and recreate the paper via QgsComposition's methods.

share|improve this answer

I am using QGIS Version 1.9.90-Alpha and Ubuntu 11.10. So it is a problem of the version - I guess?! I used your code example and it is working now:

file = QFile(self.plugin.rootDirectory + "ui/PrintTemplates/" + self.printTemplate)
doc = QDomDocument()
doc.setContent(file, False)
docElem = doc.documentElement()
compelm = doc.elementsByTagName("Composition").at(0).toElement()
self.composition.readXML(compelm, doc)

Thanks a lot!

share|improve this answer
Thanks for posting the solution. Please consider upvoting and accepting useful answers to reward people who helped you. – underdark May 18 '12 at 16:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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