0

I have created a PyQGIS script that builds a Print Layout with various text labels that are rotated to read vertically on the page. The script creates the labels and places them just fine. But upon performing a view Refresh or even exporting the Layout to PDF, all the vertical labels reset to horizontal.

This is my first foray into building Print Layouts with scripting, so I'm sure I'm missing something basic. But I'm really in a bind if I can't get this to work properly!

I'm running QGIS 3.18.2-Zürich on Windows 10.

The code I'm using to place the labels is:

# Place vertical label
newLabel = QgsLayoutItemLabel(layout)
newLabel.setText("Vertical Text Label")
newLabel.setFont(QFont('Tahoma', 14))
newLabel.setHAlign(Qt.AlignLeft)
newLabel.setVAlign(Qt.AlignTop)
newLabel.setRotation(270.0)

newLabel.attemptMove(QgsLayoutPoint(4.25, 5.50, 3))

newLabel.adjustSizeToText()
layout.addLayoutItem(newLabel)

1 Answer 1

2

You need to change method to rotate item and not text by using setItemRotation instead of setRotation

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

newLabel = QgsLayoutItemLabel(layout)
newLabel.setText("Vertical Text Label")
newLabel.setFont(QFont('Tahoma', 14))
newLabel.setHAlign(Qt.AlignLeft)
newLabel.setVAlign(Qt.AlignTop)
# newLabel.setRotation(270.0)
# Instead of above statement, do the following
newLabel.setItemRotation(270.0)

newLabel.attemptMove(QgsLayoutPoint(4.25, 5.50, 3))

newLabel.adjustSizeToText()
layout.addLayoutItem(newLabel)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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