PyQt print QWidget

后端 未结 1 1484
我寻月下人不归
我寻月下人不归 2020-12-21 05:08

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

相关标签:
1条回答
  • 2020-12-21 05:40

    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
    
    0 讨论(0)
提交回复
热议问题