PYQGIS Composer 标签对齐

PYQGIS Composer Label Alignment

我一直在为 QGIS 编写一些脚本,以便通过 composer 自动生成地图集。

我遇到的唯一问题是无法正确对齐作曲家标签。这是代码示例:

composerLabel = QgsComposerLabel(c)
newFont = QFont("times", 40)
composerLabel.setFont(newFont) 
composerLabel.setText("Hello world")
composerLabel.adjustSizeToText()
composerLabel.setItemPosition(c.paperWidth() / 2,0)

composerLabel.setHAlign(Qt.AlignCenter)

c.addItem(composerLabel)

Here's what the image output looks like

我查看了 API 并搜索了 PYQGIS 论坛,但没有其他人听起来像他们有类似的对齐问题。谁能看出我可能哪里出错了?

只需要在setItemPosition方法中添加ItemPositionMode即可

# add label
composerLabel = QgsComposerLabel(c)
newFont = QFont("times", 40)
composerLabel.setFont(newFont) 
composerLabel.setText("Hello world")
composerLabel.adjustSizeToText()
composerLabel.setItemPosition(c.paperWidth() / 2,0,QgsComposerItem.UpperMiddle)

composerLabel.setHAlign(Qt.AlignCenter)

c.addComposerLabel(composerLabel)