【问题标题】:Scaling and printing PyQt缩放和打印 PyQt
【发布时间】:2015-02-20 20:27:37
【问题描述】:

我正在尝试将主窗口及其小部件打印到纸上。理想情况下,它应该适合页面。 我尝试了以下代码,但无济于事!有没有人知道怎么做?

def printViewCustomer(self):
    printer=QtPrintSupport.QPrinter()
    dialog=QtPrintSupport.QPrintDialog(printer,self)
    if (dialog.exec_()!=QtWidgets.QDialog.Accepted):
        return
    printWidget=self.mainViewWidget
    painter=QtGui.QPainter(printer)
    painter.begin(printer)
    print(printer.paperRect().x())
    XScale=(printer.pageRect().width()/ (printWidget.width()))
    YScale=(printer.pageRect().height()/(printWidget.height()))
    Scale=(min(XScale,YScale))
    painter.translate((printer.paperRect().x()) + (printer.pageRect().width()/2),(printer.paperRect().y) + (printer.pageRect().height()/2))
    painter.scale(Scale,Scale)
    painter.translate(-1*printWidget.width()/2,-1*printWidget.height()/2)
    printWidget.render(painter)
    painter.end()

这是一个尝试的翻译

QPainter painter;
        painter.begin(&printer);
        double xscale = printer.pageRect().width()/double(myWidget->width());
        double yscale = printer.pageRect().height()/double(myWidget->height());
        double scale = qMin(xscale, yscale);
        painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
                           printer.paperRect().y() + printer.pageRect().height()/2);
        painter.scale(scale, scale);
        painter.translate(-width()/2, -height()/2);

        myWidget->render(&painter);

【问题讨论】:

  • “无济于事”究竟是什么意思?哪里失败了?
  • @ChristopherPeterson 嗨,这是我得到的错误,我怀疑它的原因是我对 C++ 没有任何了解,并试图翻译上面的内容,但弄错了! Traceback (most recent call last): painter.translate((printer.paperRect().x()) + (printer.pageRect().width()/2),(printer.paperRect().y) + (printer.pageRect().height()/2)) TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'float'

标签: python printing pyqt pyqt5


【解决方案1】:

让我们考虑一下错误:

TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'float' 

在我们做任何事情之前,请先阅读它。 “+ 不支持的操作数类型”。好吧,这听起来像是 Python 试图添加两件事,而其中之一无法添加。阅读更多内容,'builtin_function_or_method' 和 'float'。听起来您正在尝试将浮点数添加到方法/函数中,这没有任何意义。

所以只是为了修复你得到的错误:

painter.translate((printer.paperRect().x()) + (printer.pageRect().width()/2),(printer.paperRect().y) + (printer.pageRect().height()/2))

应该是

painter.translate((printer.paperRect().x()) + (printer.pageRect().width()/2), (printer.paperRect().y()) + (printer.pageRect().height()/2))

请注意,您忘记调用函数 y,而是尝试使用 y 的值,它是指向函数的指针。你可能会忘记 Qt 使用方法来访问这样的变量是可以原谅的。允许properties 访问可能更符合pythonic,但这是因为PySide/PyQt 包装了C++。

一旦您发布错误,您的代码有什么问题就很清楚了 - 请尽量记住在将来包含此类内容以节省他人的时间。

现在怎么样了?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多