TransWikia.com

Displaying map title in PyQGIS

Geographic Information Systems Asked by Marcelo Freitas on December 29, 2020

I have the following map but I don’t know how to make a title appear on top of it. Any suggestion on how to?

enter image description here

One Answer

Depending of your requirements, you may want to use a QgsLayoutItemLabel or a QgsLayoutItemHtml. To illustrate, we use QgsLayoutItemLabel. For QgsLayoutItemHtml, you may look at this existing question and its answers

from qgis.core import QgsLayoutItemLabel, QgsLayoutPoint, QgsLayoutSize
from qgis.PyQt.QtCore import Qt

project = QgsProject.instance()
manager = project.layoutManager()
layout = manager.layoutByName('demo')

new_layout_item_label = QgsLayoutItemLabel(layout)
layout.addLayoutItem(new_layout_item_label)
new_layout_item_label.setText("My title")
new_layout_item_label.setHAlign(Qt.AlignCenter)
new_layout_item_label.setVAlign(Qt.AlignVCenter)

new_layout_item_label.attemptResize(QgsLayoutSize(page_size.width(), 20, QgsUnitTypes.LayoutMillimeters))
new_layout_item_label.attemptMove(QgsLayoutPoint(0, 0, page_size.units()))

Answered by ThomasG77 on December 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP