我正在尝试在PyQt中打印小部件,但收到“ QPaintDevice:无法销毁正在绘制的绘制设备”的错误。我认为问题是我的方法结束了,因此在绘画者完成绘制像素图之前,qPaintDevice被破坏了。但是,我不知道如何降低画家的速度。
我的方法的代码在这里:
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)
对于它的价值,我尝试使用.begin()和.end()方法,但无济于事。
我发现了我的问题-我忘记了删除画家,这在事后看来很明显(不是总是这样吗?)。最后添加“ del painter”可使代码正常工作。这是工作代码:
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