PyQt print QWidget

╄→гoц情女王★ 提交于 2020-01-24 10:45:25

问题


Am trying to follow the documentation for printing a QWidet and am getting an error. When I run the following code I get QPaintDevice: Cannot destroy paint device that is being painted.

import sys
from PyQt4 import QtGui, QtCore

class SampleApp(QtGui.QDialog):
    def __init__(self):
        super().__init__()

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)

        text_editor = QtGui.QTextEdit()
        layout.addWidget(text_editor)

        button = QtGui.QPushButton("Print")
        layout.addWidget(button)
        button.clicked.connect(self.print_me)

    def print_me(self):
        printer = QtGui.QPrinter()
        printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
        printer.setOutputFileName("Test.pdf")

        self.painter = QtGui.QPainter(printer)
        margins = printer.getPageMargins(QtGui.QPrinter.DevicePixel)
        xscale = (printer.pageRect().width() - margins[0]) / self.width()
        yscale = (printer.pageRect().height() - margins[1]) / self.height()
        scale = min(xscale, yscale)
        self.painter.scale(scale, scale)

        self.render(self.painter)

app = QtGui.QApplication(sys.argv)
ex = SampleApp()
ex.show()
sys.exit(app.exec_())

If I change the print_me() method to the following, it does however work (I just lose all of the ability to scale the painter of course):

def print_me(self):
    printer = QtGui.QPrinter()
    printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
    printer.setOutputFileName("Test.pdf")

    self.render(QtGui.QPainter(printer))

回答1:


QPainter for optimization does not apply all the tasks at the same time but it keeps instructions and applies them at the end but to force that task it is better to call the end() method or delete with it since the destroyer also calls end(), in addition it is not necessary for QPainter to be a member of the class:

def print_me(self):
    printer = QtGui.QPrinter()
    printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
    printer.setOutputFileName("Test.pdf")

    painter = QtGui.QPainter(printer)
    xscale = printer.pageRect().width() / self.width()
    yscale = printer.pageRect().height() / self.height()
    scale = min(xscale, yscale)
    painter.translate(printer.paperRect().center())
    painter.scale(scale, scale)
    painter.translate((self.width() / 2) * -1, (self.height() / 2) * -1)

    self.render(painter)
    painter.end()
    # or
    # del painter


来源:https://stackoverflow.com/questions/58667932/pyqt-print-qwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!