[PyQt] Printing in PyQt

David Boddie david at boddie.org.uk
Sat Feb 21 15:05:00 GMT 2015

On Fri, 20 Feb 2015 11:31:10 -0800 (PST), primalcharlie wrote:

> Hi Im hoping to print a whole screen and as a separate print a widget:
> However I have no clue as to how to do it. I know that I need to do a paint
> and paint to the printer and get the details using a QPrintDialog however
> past that point my knowledge is limited. I found the following on the
> internet in the pyqt docs but it isn't working

Can you give a bit more details about how it fails to produce what you

> 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()

Have you seen the examples provided with PyQt5? There should be ones that
cover printing.


More information about the PyQt mailing list