在 PyQt 中打印图像时出错 [英] Error printing image in PyQt

查看:55
本文介绍了在 PyQt 中打印图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 PyQt 中打印一个小部件,但收到QPaintDevice:无法销毁正在绘制的绘制设备"的错误消息.我认为问题是我的方法结束了,因此在画家完成像素图绘制之前,qPaintDevice 就被销毁了.但是,我不知道如何减慢画家的速度.

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 method ends, and therefore the qPaintDevice is destroyed, before the painter has finished drawing the pixmap. I have no idea how to slow the painter down, however.

我的方法的代码在这里:

The code for my method is here:

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() 方法,但无济于事.

For what it's worth, I tried using the .begin() and .end() approach, to no avail.

推荐答案

我发现了我的问题——我忘记删除画家了,事后看来这很明显(不是总是如此吗?).在末尾添加delpainter"使代码工作.这是工作代码:

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

这篇关于在 PyQt 中打印图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆