6

I am facing a problem with the new Layout API while implementing a plugin for QGIS 3. Starting point is a layout template in qpt-Format, which contains different elements like (attribute) tables, map, labels and scale-bar. This template is loaded into the Layout Manager with the code below.

During import I want to make dynamic changes to the layout elements like filling tables with new rows or editing the label text.

Any idea on how to address elements of the layout during or after the import, in order to edit the content of labels and tables?

project = QgsProject.instance()
composition = QgsPrintLayout(project)
document = QDomDocument()

# read template content
template_file = open('C:/location/of/template.qpt')
template_content = template_file.read()
template_file.close()
document.setContent(template_content)

# load layout from template and add to Layout Manager
composition.loadFromTemplate(document, QgsReadWriteContext()) 
project.layoutManager().addLayout(composition)

# maybe using QgsLayoutItem???
# layoutItem = composition.itemById("1")
0

First you need to get the layout as it has been added.

layout = QgsProject.instance().layoutManager().layoutByName("My comp name")

Then with this class can access to your table item by requesting its id if you had set one.

my_table = layout.itemById()

Then working on this particular item like check data or add data. I did'nt use it.

my_table.data(0)
my_table.data(0, "some data")

Hope It gave you some hint to start working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.