0

我正在尝试将文本集中在普通 A4 尺寸的作品集中,获得的尺寸是正确的,但它没有居中,我不知道原因是什么,这里我向您展示代码

from PyQt4.QtGui import QPrinter,QPainter,QApplication
from PyQt4.QtCore import Qt,QRectF
import sys

app = QApplication(sys.argv)

printer = QPrinter()
painter = QPainter()

printer.setOutputFileName("prueba.pdf")
printer.setOutputFormat(QPrinter.PdfFormat)

printer.setPageMargins(0.0,0.0,0.0,0.0,1)
printer.setFullPage(True)
margin = printer.getPageMargins(1)
print margin
size = printer.paperSize(1)
width = size.width()
print width

painter.begin(printer)
painter.drawText(QRectF(0.0,0.0,width,50.0),Qt.AlignCenter|Qt.AlignTop,"abcdefghijklmn")
painter.end()

如您所见,我得到了对开类型点大小的宽度,并且将对开的整个宽度不均匀

4

1 回答 1

0

QPrinter使用不同类型的计量单位

QPrinter.Unit

此枚举类型用于指定页面和纸张尺寸的测量单位。

Constant  Value
QPrinter.Millimeter   0
QPrinter.Point        1
QPrinter.Inch         2
QPrinter.Pica         3
QPrinter.Didot        4
QPrinter.Cicero       5
QPrinter.DevicePixel  6

当你通过paperSize()方法得到尺寸时,你要求的是QPrinter.Point单位,但是QPainter使用的是像素坐标,也就是说,QPrinter.DevicePixel得到的宽度和像素的宽度是不同的,如果我们只是改变单位的类型来解决这个问题测量如下图:

app = QApplication(sys.argv)

printer = QPrinter()
painter = QPainter()

printer.setOutputFileName("prueba.pdf")
printer.setOutputFormat(QPrinter.PdfFormat)

printer.setPageMargins(0.0, 0.0, 0.0, 0.0, QPrinter.Point)
printer.setFullPage(True)
margin = printer.getPageMargins(QPrinter.Point)
print(margin)
size = printer.paperSize(QPrinter.DevicePixel)
width = size.width()
print(width)

painter.begin(printer)
painter.drawText(QRectF(0.0,0.0,width,50.0), Qt.AlignCenter|Qt.AlignTop, "abcdefghijklmn")
painter.end()
于 2017-09-19T14:34:13.680 回答