[QGIS-Developer] Set text of QgsLayoutItemLabel

KOHLMANN Hannes H.KOHLMANN at ove.at
Fri Mar 16 08:56:06 PDT 2018


Hello!

I am facing difficulties trying to set the text of a  QgsLayoutItemLabel.
I do something like the following to load a .qpt composer template and print this to a .png file (which works)

path = "C:\\QGIS3\\apps\\qgis\\composer_templates"
template_file = open(path+"\\ANSR_print_composer2.qpt")
template_content = template_file.read()
template_file.close()

document = QDomDocument()
document.setContent(template_content)
composition = QgsLayout(QgsProject.instance())
composition.loadFromTemplate(document, QgsReadWriteContext())

map_item = composition.itemById('map')
legend_item = composition.itemById('legend')
text_item = composition.itemById('textbox')
QgsLayoutExporter(composition).exportToImage( path+"\\report.png", QgsLayoutExporter.ImageExportSettings())

And now I'd like to modify only the text of the QgsLayoutItemLabel, which is the "textbox":


1)      I can move it around with setX(), setY(), but I don't know how to edit the text of the label. I thought of something like text_item.setText() or text_item.setLabelText(), but I think that text_item is just an instance of QgsLayoutItem, not QgsLayoutItemLabel. And there I get stuck.

2)      How would you manually add another text label, set the main property etc, such that, at the end of the day, my above code would export a .png containing this QgsLayoutItem?
I would have begun this way:
new = QgsLayoutItemLabel(composition)
new.setText("New label")
But it does not quite work that way!

Would be of great help, if you could give me some hints on that.

Thank you in advance! :)

Hannes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180316/a29c8faf/attachment.html>


More information about the QGIS-Developer mailing list