我正在尝试在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()方法,但无济于事。
答案 0 :(得分:6)
我想出了我的问题 - 我忘了删除画家,事后看来这很明显(不是总是这样吗?)。将“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