2

In a QGis python plugin i have this code to fill in the fields of a PDF template (.qpt):

    myComposition = QgsComposition(myMapRenderer)
    myFile = path + "template.qpt"
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent)
    myComposition.loadFromTemplate(myDocument)

    item = myComposition.getComposerItemById("id_field")
    item.setText("my value")

working with QGis 2.18.7 item is a qgis._core.QgsComposerLabel and the method setText() is correct.

working with QGis 2.18.13 item is a qgis._core.QgsComposerItem and the method setText() not exist.

Is getComposerItemById changed with the QGIS version?

1

1 Answer 1

1

SOLVED

whit this code:

    [...]
    item = myComposition.getComposerItemById("id_field")
    item = self.bugWAGetComposerLabel(item)
    item.setText("my value")
    [...]


def bugWAGetComposerLabel(self, item):
    if not item.__class__ == QgsComposerLabel:
        if item.type() == QgsComposerItem.ComposerLabel:
            item.__class__ = QgsComposerLabel
    return item

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.