I want to preview, and then print, a report through a printer using PyQt. I tried the following code :
printer = QtGui.QPrinter()
doc = QtGui.QTextDocument("testing")
dialog = QtGui.QPrintDialog(printer)
dialog.setModal(True)
dialog.setWindowTitle("printerrr")
pdialog = QtGui.QPrintPreviewDialog(printer)
pdialog.setWindowFlags(QtCore.Qt.Window)
pdialog.exec_()
How I can preview my report then print it?
Basic demo of Qt’s print dialogs:
UPDATE:
An alternative solution to the ones given below is to use Qt WebEngine to render and print the documents. This allows a much greater range of document types to be supported in addition to HTML, such as PDFs, images, plain text, etc. The
Printerclass in the following example is based on the WebEngine Widgets PrintMe Example from the Qt docs.Previous examples:
PyQt4:
PyQt5:
To print-preview a graphics view, use its
rendermethod: