打印机QPrinter及实例
QPrinter 表示打印出来的纸张,可以作为QPainter的绘图设备来使用,QPainter 可以按照QPrinter绘制图形、文字和图像等内容。
QPrinter 继承自QPagedPaintDevice 和 QPaintDevice。用QPrinter 创建打印机实例的方法如下所示,其中参数 mode 设置打印模式,取值是 QPrinter.PrinterMode的枚举值,可取以下值,取不同的值会影响QPainter的视口。
- QPrinter.ScreenResolution(使用屏幕分辨率)
- QPrinter.HighResolution
from PySide6.QtPrintSupport import QPrinter
QPrinter(mode: PySide6.QtPrintSupport.QPrinter.PrinterMode=Instance(PySide6.QtPrintSupport.QPrinter.PrinterMode.ScreenResolution))-> None
QPrinter(printer: PySide6.QtPrintSupport.QPrinterInfo,
mode: PySide6.QtPrintSupport.QPrinter.PrinterMode=Instance(PySide6.QtPrintSupport.QPrinter.PrinterMode.ScreenResolution))-> None
打印机QPrinter的常用方法
打印机QPrinter 的常用方法如表:
QPrinter的方法及参数类型 | 返回值的类型 | 说 明 |
---|---|---|
setPrinterName(str) | None | 设置打印机名称 |
printerName() | str | 获取打印机名称 |
setOutputFileName(str) | None | 设置打印到文件的文件名 |
outputFileName() | str | 获取打印文件名 |
setOutputFomat(QPrinter.OutputFormat) | None | 设置打印到文件时的格式 |
setFullPage(bool) | None | 设置是否整页模式打印 |
setPageMargins(margins: Union[QMarginsF,QMargins],units: QPageLayout.Unit=QPageLayout.Millimeter) | bool | 设置打印页边距 |
setCopyCount(int) | None | 设置打印份数 |
copyCount() | int | 获取打印份数 |
setCollateCopies(collate:bool) | None | 设置是否对照打印 |
collateCopies() | bool | 获取对照打印 |
setPromTo(fromPage: int,toPage:int) | None | 设置打印页数的范围 |
fromPage() | int | 获取打印范围起始页 |
toPage() | int | 获取打印范围终止页 |
setPageOrder(QPrinter.PageOrder) | None | 设置打印顺序 |
pageOrder() | QPrinter.PageOrder | 获取打印顺序 |
setResolution(int) | None | 设置打印精度 |
resolution() | int | 获取打印精度 |
setPageOrientation(QPageLayout.Orientation) | bool | 设置打印方向 |
newPage() | bool | 生成新页 |
abort() | bool | 取消正在打印的文档 |
fllPage() | bool | 获取是否整页模式 |
isValid() | bool | 获取打印机是否有效 |
PaPerRect(QPrinter.Unit) | QRectF | 获取纸张范围 |
setPrintRange(tange: QPrinter.PrintRange) | None | 设置打印范围的模式 |
printRange() | QPrinter.PrintRange | 获取打印范围的模式 |
printerState() | QPrinter.PrinterState | 获取打印状态 |
serColorMode(QPrinter.ColorMode) | None | 设置颜色模式 |
colorMode() | QPrinter.ColorMode | 获取颜色模式 |
setDocName(str) | None | 设置打印来源文档名 |
docName() | str | 获取文档名 |
setDuplex(duplex: QPrinter.DuplexMode) | None | 设置双面模式 |
duplex() | QPrinter.DuplexMode | 获取双面模式 |
setFontEmbeddingEnabled(enable: bool) | None | 设置是否启用内置字体 |
fontEmbeddingEnabled() | b001 | 获取是否启用内置字体 |
setPageSize(Union[QPageSize,QPageSize.PageSizeId,QSize]) | bool | 设置打印纸的尺寸 |
pageRect(QPrinter.Unit) | QRectF | 获取打印范围 |
setPaperSource(QPrinter.PaperSource) | None | 设置纸张来源 |
paperSource() | QPrinter.PaperSource | 获取纸张来源 |
supportedPaperSources() | List[QPrinter.PaperSource] | 获取支持的纸张来源列表 |
setPdfVersion(QPagedPaintDevice.PdfVersion) | None | 设置 pdf文档的版本 |
setPageRanges(ranges:QPageRanges) | None | 设置选中的页数 |
pageRanges() | QPageRanges | 获取选中的页数对象 |
setPageLayout(pageLayout: QPageLayout) | bool | 设置页面布局 |
pageLayout() | QPageLayout | 获取页面布局对象 |
supportedResolutions() | List[int] | 获取打印机支持的分辨率列表 |
supportsMultipleCopies() | bool | 获取是否支持多份打印 |
打印机QPrinter主要方法介绍如下。
-
用QPrinterInfo的静态方法availablePrinterNames()可以获得本机已经安装的打印机名称列表,然后用QPrinter 的setPrinterName(str)方法将其中一台打印机设置成QPrinter。
-
用setOutputFileName(str)方法设置打印机将打印内容打印到文件中而不是纸质介质上
- 参数是文件名。如果不设置该内容或设置为空字符串,则将内容打印到纸张上;
- 如果设置了扩展名是pdf的文件名,则将打印内容输出到pdf文档中;
- 如果设置的扩展名不是pdf,则将打印内容输出到按照 setOutputFormat(QPrinter.OutputFormat)方法设置的文件格式的文件中,其中QPrinter.OutputFormat枚举值可取:
- QPrinter.NativeFormat(本机定义格式,值是0)
- QPrinter.PdfFormat(pdf格式,值是1)。
-
在打印完第一页后,需要用newPage()方法通知打印机弹出当前正在打印的纸张,继续进行下一页的打印,成功则返回True;用abort()方法取消当前的打印,成功则返回True。
-
用setFullPage(bool)方法设置是否是整页模式打印。
-
在setFullPage(True)时,QPainter的坐标原点在可打印区的左上角,与纸张坐标系的原点重合,如图所示,但由于页边距的限制,实际上不能在整张纸上打印;
-
在 setFullPage(False)时,QPainter的坐标原点在打印区域的左上角。
-
页边距用setPageMargins(margins: Union[QMarginsF,QMargins],units: QPageLayout.Unit=QPageLayout,Millimeter)方法设置,其中单位 Unit 可取:
- QPageLayout.Millimeter,QPageLayout.Point(=in/72)、
- QPageLayout.Inch、QPageLayout.Pica(=in/6)
- QPageLayout.DidQt(=0.375mm)
- QPageLayout.Cicero(=4.5mm)
对应值分别是0~6。
-
用paperRect(QPrinter,Unit)方法和 pageRect(QPrinter.Unit)方法可以分别获取纸张和打印区的矩形区域QRectF,
- QPrinter.Unit可取以下值,对应值分别是0~6。
- QPrinter.Millimeter
- QPrinter.Point
- QPrinter.Inch
- QPrinter.Pica
- QPrinter.DidQt
- QPrinter,Cicero
- QPrinter.DevicePixel
- QPrinter.Unit可取以下值,对应值分别是0~6。
-
-
用setCopyCount(int)方法设置打印份数;
- 用setCollateCopies(collate:bool)方法设置是否对照打印,在setCollateCopies(False)时,每张都会连续打印指定的份数。
-
用setResolution(int)方法设置打印精度(分辨率),单位是dpi(dots per inch)。
-
用setPageOrientation(QPageLayout.Orientation)方法设置打印方向,参数是·QPageLayout.Orientation 的枚举值,可取
QPageLayout.Portrait
(纵向,值是0)、QPageLayout.Landscape
(横向,值是1)-
用setPageOrder(QPrinter.PageOrder)方法设置打印顺序,参数是QPrinter.PageOrder的枚举值,可取:
- QPrinter.FirstPageFirst(正常顺序,值是0)
- QPrinter.LastPageFirst(反向顺序,值是1)。
-
-
用setPrintRange(range:QPrinter.PrintRange)方法设置打印范围的模式,参数range 可取:
- QPrinter.AllPages(打印所有页,值是0)
- QPrinter.Selection(打印选中的页,值是1)
- QPrinter.PageRange(打印指定范围的页,值是2)
- QPrinter.CurrentPage(打印当前页,值是3);
-
用setFromTo(fromPage: int,toPage:int)方法或setPageRanges(ranges: QPageRanges)方法设置打印范围
- 参数ranges是QPageRanges的对象,可以用QPageRanges的addPage(pageNumber:int)方法或addRange(from_: int,to: int)方法添加要打印的页;
- 用clear()方法清除已经添加的页数;
- 用静态方法fromString(ranges:str)将字符串转换成QPageRanges对象,QPageRanges类在QtGui 模块中。
-
用setPageSize(Union[QPageSize,QPageSize.PageSizeId,QSize])方法可以设置纸张的尺寸,其中枚举值QPageSize.PageSizeld定义了一些常用的纸张尺寸,常用的纸张尺寸如表所示:
-
也可用QPageSize(pageSizeId: QPageSize.PageSizeId)、QPageSize(size:Union[QSizeF,QSize],units: QPageSize.Unit)方法定义新的尺寸。
-
用QPageSize的size(units:QPageSize.Unit)方法获取纸张尺寸 QSizeF;
-
用sizePixels(resolution: int)方法获取用像素表示的尺寸QSize,或用sizePoints()方法获取用点表示的尺寸QSize。
QPageSize.PageSizeId 的取值 值 尺寸/(mmXmm) QPageSize.PageSizeId 的取值 值 尺寸/(mmXmm) QPageSize.Letter 0 215.9×279.4 QPageSize.B2 16 500×707 QPageSize.Legal 1 215.9×355.6 QPageSize.B3 17 353X500 QPageSize.Executive 2 190.5×254 QPageSize.B4 18 250X353 QPageSize.A0 3 841X1189 QPageSize.B5 19 176X250 QPageSize.Al 4 594×841 QPageSize.B6 20 125×176 QPageSize.A2 5 420×594 QPageSize.B7 21 88X125 QPageSize.A3 6 297×420 QPageSize.B8 22 62×88 QPageSize.A4 7 210×297 QPageSize.B9 23 44X62 QPageSize.A5 8 148X210 QPageSize.B10 24 31X44 QPageSize.A6 9 105×148 QPageSize.C5E 25 163X229 QPageSize.A7 10 74X105 QPageSize.Col0E 26 105×241 QPageSize.A8 11 52×74 QPageSize.DLE 27 110X220 QPageSize.A9 12 37X52 QPageSize.Folio 28 210X330 QPageSize.BO 14 1000×1414 QPageSize.Ledger 29 431.8×279.4 QPageSize,B1 15 707×1000 QPageSize.Tabloid 30 279.4×431.8
-
-
用setPaperSource(QPrinter.PaperSource)方法设置纸张来源(位置),参数可取:
- QPrinter.Auto
- QPrinter.Cassette
- QPrinter.Envelope
- QPrinter.EnvelopeManual
- QPrinter.FormSource
- QPrinter.LargeCapacity
- QPrinter.LargeFormat
- QPrinter.Lower
- QPrinter.MaxPageSource
- QPrinter.Middle
- QPrinter.Manual
- QPrinter.OnlyOne
- QPrinter.Tractor
- QPrinter.SmallFormat
- QPrinter.Upper
- QPrinter.CustomSource
- QPrinter.LastPaperSource
用supportedPaperSources()方法获取打印机支持的纸张来源列表。
-
用setPageLayout(pageLayout:QPageLayout)方法为打印机设置页面布局页面布局QPageLayout 用于设置页面的方向、页面尺寸和页边距。
-
用QPageLayout 创建页面布局的方法是以下方法:
from PySide6.QtGui import QPageLayout QPageLayout(self)-> None QPageLayout(Other: PySide6.QtGui.QPageLayout)-> None QPageLayout(pageSize: Union[PySide6.QtGui.QPageSize,PySide6.QtGui.QPageSize.PageSizeId,PySide6.QtCore.QSize], orientation: PySide6.QtGui.QPageLayout.Orientation, margins: Union[PySide6.QtCore.QMarginsF,PySide6.QtCore.QMargins], units: PySide6.QtGui.QPageLayout.Unit=Instance(QPageLayout.Unit.Point), minMargins: Union[PySide6.QtCore.QMarginsF, PySide6.QtCore.QMargins]=Instance(QMarginsF(0,0,0,0)))-> None
-
利用QPageLayout 的 setPageSize(pageSize: Union[QPageSize,QPageSize.PageSizeld,QSize],minMargins: Union[QMarginsF,QMargins]=QMarginsF(0,0,0,0))方法可以设置纸张尺寸;
-
用setOrientation(orientation:QPageLayout.Orientation)方法设置方向(横向或纵向);
-
用setMargins(margins:Union[QMarginsF,QMargins])方法设置 页边距;
-
用setUnits(units:QPageLayout.Unit)方法设置单位;
-
用setMode(mode:QPageLayout.Mode)方法设置布局模式,参数mode 可取:
- QPageLayout.StandardMode(打印范围包含页边距,值是0)
- QPageLayout.FullPageMode(打印范围不含页边距,值是1)
-
实例
运行下面的程序,选择合适的打印机、设置打印份数,并可选择是否打印到文件,单击
打印"按钮后开始打印。在每页上打印一个五角星。
import sys
from PySide6.QtWidgets import QApplication,QWidget,QComboBox,QPushButton,QCheckBox,QLineEdit,QSpinBox,QFormLayout
from PySide6.QtPrintSupport import QPrinter,QPrinterInfo
from PySide6.QtGui import QPen,QPainter,QPageSize,QPageLayout
from PySide6.QtCore import QPointF,QPoint
from math import cos,sin,pi
class MyWidget(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
self.comboBox=QComboBox()
self.comboBox.currentTextChanged.connect(self.comboBox_currentText)# 信号与槽连接
self.spin_copies=QSpinBox()
self.spin_copies.setRange(1,100)
self.checkBox=QCheckBox('输出到文件')
self.checkBox.clicked.connect(self.checkBox_clicked)
self.line_file=QLineEdit()
self.line_file.setText('d:/stars.pdf')
self.line_file.setEnabled(self.checkBox.isChecked())
self.btn_printer=QPushButton('打印')
self.btn_printer.clicked.connect(self.btn_printer_clicked)# 信号与槽连接
formLayout=QFormLayout(self)
formLayout.addRow("选择打印机:",self.comboBox)
formLayout.addRow("设置打印份数:",self.spin_copies)
formLayout.addRow(self.checkBox,self.line_file)
formLayout.addRow(self.btn_printer)
printerNames=QPrinterInfo.availablePrinterNames()
self.comboBox.addItems(printerNames)
self.comboBox.setCurrentText(QPrinterInfo.defaultPrinterName())
def comboBox_currentText(self,text): # 槽函数
printInfo=QPrinterInfo.printerInfo(text)
self.printer=QPrinter(printInfo)# 打印机
self.printer.setPageOrientation(QPageLayout.Orientation.Portrait)
self.printer.setFullPage(False)
self.printer.setPageSize(QPageSize.A4)
self.printer.setColorMode(QPrinter.ColorMode.GrayScale)
def checkBox_clicked(self,checked): # 槽函数
self.line_file.setEnabled(checked)
def btn_printer_clicked(self): # 槽函数
self.printer.setOutputFileName(None)
if self.checkBox.isChecked():
self.printer.setOutputFileName(self.line_file.text())# 设置打印到文件中if self.printer.isValid():
self.painter=QPainter()
if self.painter.begin(self.printer):
pen=QPen()# 钢笔
pen.setWidth(3)# 线条宽度
self.painter.setPen(pen)# 设置钢笔
x=self.printer.paperRect(QPrinter.Unit.DevicePixel).width()/ 2 # 中心x坐标
y=self.printer.paperRect(QPrinter.Unit.DevicePixel).height()/ 2 # 中心y坐标
r=min(self.printer.pageRect(QPrinter.Unit.DevicePixel).width()/ 2,self.printer.paperRect(QPrinter.Unit.DevicePixel).height()/ 2)# 外接圆半径
p1=QPointF(r * cos(-90 * pi / 180)+ x,r * sin(-90 * pi / 180)+ y)
p2=QPointF(r * cos(-18 * pi / 180)+ x,r * sin(-18 * pi / 180)+ y)
p3=QPointF(r * cos(54 * pi / 180)+ x,r * sin(54 * pi / 180)+ y)
p4=QPointF(r * cos(126 * pi / 180)+ x,r * sin(126 * pi / 180)+ y)
p5=QPointF(r * cos(198 * pi / 180)+ x,r * sin(198 * pi / 180)+ y)
pageCopies=self.spin_copies.value()
for i in range(1,pageCopies + 1):
self.painter.drawPolyline([p1,p3,p5,p2,p4,p1])# 绘制五角星
print("正在提交第{}页,共{}页".format(i,pageCopies))
if i !=pageCopies:
self.printer.newPage()
self.painter.end()
if __name__=='__main__':
app=QApplication(sys.argv)
win=MyWidget()
win.show()
sys.exit(app.exec())