[QGIS-Developer] Set text of QgsLayoutItemLabel

SIGéal sigeal at sigeal.com
Sat Mar 17 04:32:30 PDT 2018


Hi,

This thread might help you to find a workaround :
https://gis.stackexchange.com/questions/241213/how-to-get-a-composer-label-by-id-with-python-in-qgis

-- 
Christophe


Le 16/03/2018 à 16:56, KOHLMANN Hannes a écrit :
>
> 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! J
>
> Hannes
>
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180317/4cb32648/attachment.html>


More information about the QGIS-Developer mailing list