2

I am trying to create a composer using PyQGIS. Everything works as expected. The only difference compared to the manual creation of a composer is, that I am not able to update the layers (by pressing "Refresh View") in the Composer GUI after creating the composer by script which works fine when I create the composer the GUI-way.

My code so far:

from PyQt4.QtXml import QDomDocument

template_filePath = "PATH_TO_COMPOSER_TEMPLATE"
template_file = file(template_filePath)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition.loadFromTemplate(document)
1
  • By the way - if I close the project and restart it, the composer and the Refresh View Button behaves normal - I guess I am missing some line of code at the end which sets a specific state of the composer or something... May 3, 2016 at 14:01

1 Answer 1

2

check this:

from PyQt4.QtCore import *
from PyQt4.QtXml import *

myFile = "PATH_TO_COMPOSER_TEMPLATE"
myTemplateFile = file(myFile, 'rt')
myTemplateContent = myTemplateFile.read()
myTemplateFile.close()
myDocument = QDomDocument()
myDocument.setContent(myTemplateContent, False)
newcomp = iface.createNewComposer()
newcomp.composition().loadFromTemplate(myDocument)
composition = QgsComposition(iface.mapCanvas().mapSettings())
composition.refreshItems()

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.