1

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

Taras
  • 32,823
  • 4
  • 66
  • 137
  • Not a definite answer, but here you have an example: https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/cheat_sheet.html#decorators – Germán Carrillo May 31 '20 at 15:36

1 Answers1

1

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()))
ThomasG77
  • 30,725
  • 1
  • 53
  • 93