Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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()
composition.clear()
paper = QgsPaperItem(composition)
paper.readXML(docElem, doc)
composition.addItem(paper)
composition.moveItemToBottom(paper)
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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.