Error printing image in PyQt

前端 未结 1 400
孤独总比滥情好
孤独总比滥情好 2020-12-21 04:01

I\'m trying to print a widget in PyQt, but am getting the error that \"QPaintDevice: Cannot destroy paint device that is being painted\". I think the problem is that my meth

相关标签:
1条回答
  • 2020-12-21 04:27

    I figured out my problem -- I was forgetting to delete the painter, which in hindsight seems obvious (doesn't it always?). Adding "del painter" to the end makes the code work. Here is working code:

    def printer(self):
        "Prints the current diagram"
        # Create the printer
        printerobject = QtGui.QPrinter(0)
        # Set the settings
        printdialog = QtGui.QPrintDialog(printerobject)
        if printdialog.exec_() == QtGui.QDialog.Accepted:
            # Print
            pixmapImage = QtGui.QPixmap.grabWidget(self.contentswidget)
            painter = QtGui.QPainter(printerobject)
            painter.drawPixmap(0, 0, pixmapImage)
            del painter
    
    0 讨论(0)
提交回复
热议问题