I'm looking for a way to edit the title and subtitle from a .qpt
layout with the text entered in two QlineEdit.
The code below works if I run it once but nothing happens when I run it twice...
def qpt_loading(self):
project = QgsProject.instance()
for filename in glob.glob(self.plugin_path + "/layout_models/*.qpt"):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
layout = QgsPrintLayout(project)
layout.initializeDefaults()
template_content = f.read()
doc = QDomDocument()
doc.setContent(template_content)
layout.loadFromTemplate(doc, QgsReadWriteContext(), True)
layout.setName(os.path.basename(filename))
if layout.name() == "layout_model_A3_landscape_B1":
## adding title from lineEdit
title = QgsLayoutItemLabel(layout)
layout.addLayoutItem(title)
title_text = self.dlg.lineEdit_2.text()
title.setText(title_text)
title.setFont(QFont("Calibri", 20))
title.adjustSizeToText()
title.attemptMove(QgsLayoutPoint(182, 5, QgsUnitTypes.LayoutMillimeters))
title.adjustSizeToText()
layout.addItem(title)
title.setHAlign(Qt.AlignCenter)
title.setVAlign(Qt.AlignVCenter)
## adding subtitle from lineEdit
subtitle = QgsLayoutItemLabel(layout)
layout.addLayoutItem(subtitle)
subtitle_text = self.dlg.lineEdit_3.text()
subtitle.setText(subtitle_text)
subtitle.setFont(QFont("Calibri", 16))
subtitle.adjustSizeToText()
subtitle.attemptMove(QgsLayoutPoint(182, 18, QgsUnitTypes.LayoutMillimeters))
subtitle.adjustSizeToText()
layout.addItem(subtitle)
project.layoutManager().addLayout(layout)
updated_layout = QgsProject.instance().layoutManager().layoutByName("layout_model_A3_landscape_B1")
map_item = layout_modifie.itemById("Map 1")
# map_item = layout_modifie.referenceMap()
map_item.zoomToExtent(iface.mapCanvas().extent())
iface.openLayoutDesigner(updated_layout )