Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:39+00:00 2026-05-26T22:57:39+00:00

I want to preview, and then print, a report through a printer using PyQt.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:57:40+00:00Added an answer on May 26, 2026 at 10:57 pm

    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 Printer class in the following example is based on the WebEngine Widgets PrintMe Example from the Qt docs.

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5 import QtPrintSupport, QtWebEngineWidgets
    
    
    class Window(QtWidgets.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.viewer = QtWebEngineWidgets.QWebEngineView(self)
            self.viewer.settings().setAttribute(
                QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
            self.viewer.settings().setAttribute(
                QtWebEngineWidgets.QWebEngineSettings.PdfViewerEnabled, True)
            self.buttonOpen = QtWidgets.QPushButton('Open', self)
            self.buttonOpen.clicked.connect(self.handleOpen)
            self.buttonPrint = QtWidgets.QPushButton('Print', self)
            self.buttonPrint.clicked.connect(self.handlePrint)
            self.buttonPreview = QtWidgets.QPushButton('Preview', self)
            self.buttonPreview.clicked.connect(self.handlePreview)
            layout = QtWidgets.QGridLayout(self)
            layout.addWidget(self.viewer, 0, 0, 1, 3)
            layout.addWidget(self.buttonOpen, 1, 0)
            layout.addWidget(self.buttonPrint, 1, 1)
            layout.addWidget(self.buttonPreview, 1, 2)
            self.printer = Printer(self.viewer.page(), self)
    
        def handleOpen(self):
            if path := QtWidgets.QFileDialog.getOpenFileName(self)[0]:
                self.viewer.load(QtCore.QUrl.fromLocalFile(path))
    
        def handlePrint(self):
            if self.viewer.url().isValid():
                self.printer.print()
    
        def handlePreview(self):
            if self.viewer.url().isValid():
                self.printer.preview()
    
    
    class Printer(QtCore.QObject):
        def __init__(self, page, parent=None):
            super().__init__(parent)
            self._page = page
            self._printing = False
    
        def render(self, printer):
            loop = QtCore.QEventLoop()
            failed = False
            QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
            def callback(result):
                nonlocal failed
                failed = not result
                QtWidgets.QApplication.restoreOverrideCursor()
                loop.quit()
            self._page.print(printer, callback)
            loop.exec()
            if failed:
                painter = QtGui.QPainter()
                if painter.begin(printer):
                    font = painter.font()
                    font.setPixelSize(20)
                    painter.setFont(font)
                    painter.drawText(QtCore.QPointF(10, 25),
                        'Could not generate print preview')
                    painter.end()
    
        def preview(self):
            if not self._printing:
                self._printing = True
                printer = QtPrintSupport.QPrinter()
                printer.setResolution(300)
                dialog = QtPrintSupport.QPrintPreviewDialog(
                    printer, self._page.view())
                dialog.paintRequested.connect(self.render)
                dialog.exec()
                dialog.deleteLater()
                self._printing = False
    
        def print(self):
            printer = QtPrintSupport.QPrinter(
                QtPrintSupport.QPrinter.HighResolution)
            dialog = QtPrintSupport.QPrintDialog(printer, self._page.view())
            if dialog.exec() == QtWidgets.QDialog.Accepted:
                self.render(printer)
            dialog.deleteLater()
    
    
    if __name__ == '__main__':
    
        QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
        app = QtWidgets.QApplication(['Printing Demo'])
        window = Window()
        window.setGeometry(600, 100, 800, 600)
        window.show()
        app.exec()
    

    Previous examples:

    PyQt4:

        import sys, os
        from PyQt4 import QtGui, QtCore
        
        class Window(QtGui.QWidget):
            def __init__(self):
                super(Window, self).__init__()
                self.setWindowTitle('Document Printer')
                self.editor = QtGui.QTextEdit(self)
                self.editor.textChanged.connect(self.handleTextChanged)
                self.buttonOpen = QtGui.QPushButton('Open', self)
                self.buttonOpen.clicked.connect(self.handleOpen)
                self.buttonPrint = QtGui.QPushButton('Print', self)
                self.buttonPrint.clicked.connect(self.handlePrint)
                self.buttonPreview = QtGui.QPushButton('Preview', self)
                self.buttonPreview.clicked.connect(self.handlePreview)
                layout = QtGui.QGridLayout(self)
                layout.addWidget(self.editor, 0, 0, 1, 3)
                layout.addWidget(self.buttonOpen, 1, 0)
                layout.addWidget(self.buttonPrint, 1, 1)
                layout.addWidget(self.buttonPreview, 1, 2)
                self.handleTextChanged()
        
            def handleOpen(self):
                path = QtGui.QFileDialog.getOpenFileName(
                    self, 'Open file', '',
                    'HTML files (*.html);;Text files (*.txt)')
                if path:
                    file = QtCore.QFile(path)
                    if file.open(QtCore.QIODevice.ReadOnly):
                        stream = QtCore.QTextStream(file)
                        text = stream.readAll()
                        info = QtCore.QFileInfo(path)
                        if info.completeSuffix() == 'html':
                            self.editor.setHtml(text)
                        else:
                            self.editor.setPlainText(text)
                        file.close()
        
            def handlePrint(self):
                dialog = QtGui.QPrintDialog()
                if dialog.exec_() == QtGui.QDialog.Accepted:
                    self.editor.document().print_(dialog.printer())
        
            def handlePreview(self):
                dialog = QtGui.QPrintPreviewDialog()
                dialog.paintRequested.connect(self.editor.print_)
                dialog.exec_()
        
            def handleTextChanged(self):
                enable = not self.editor.document().isEmpty()
                self.buttonPrint.setEnabled(enable)
                self.buttonPreview.setEnabled(enable)
        
        if __name__ == '__main__':
        
            app = QtGui.QApplication(sys.argv)
            window = Window()
            window.resize(640, 480)
            window.show()
            sys.exit(app.exec_())
    

    PyQt5:

    import sys, os
    from PyQt5 import QtCore, QtWidgets, QtPrintSupport
    
    class Window(QtWidgets.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.setWindowTitle('Document Printer')
            self.editor = QtWidgets.QTextEdit(self)
            self.editor.textChanged.connect(self.handleTextChanged)
            self.buttonOpen = QtWidgets.QPushButton('Open', self)
            self.buttonOpen.clicked.connect(self.handleOpen)
            self.buttonPrint = QtWidgets.QPushButton('Print', self)
            self.buttonPrint.clicked.connect(self.handlePrint)
            self.buttonPreview = QtWidgets.QPushButton('Preview', self)
            self.buttonPreview.clicked.connect(self.handlePreview)
            layout = QtWidgets.QGridLayout(self)
            layout.addWidget(self.editor, 0, 0, 1, 3)
            layout.addWidget(self.buttonOpen, 1, 0)
            layout.addWidget(self.buttonPrint, 1, 1)
            layout.addWidget(self.buttonPreview, 1, 2)
            self.handleTextChanged()
    
        def handleOpen(self):
            path = QtWidgets.QFileDialog.getOpenFileName(
                self, 'Open file', '',
                'HTML files (*.html);;Text files (*.txt)')[0]
            if path:
                file = QtCore.QFile(path)
                if file.open(QtCore.QIODevice.ReadOnly):
                    stream = QtCore.QTextStream(file)
                    text = stream.readAll()
                    info = QtCore.QFileInfo(path)
                    if info.completeSuffix() == 'html':
                        self.editor.setHtml(text)
                    else:
                        self.editor.setPlainText(text)
                    file.close()
    
        def handlePrint(self):
            dialog = QtPrintSupport.QPrintDialog()
            if dialog.exec_() == QtWidgets.QDialog.Accepted:
                self.editor.document().print_(dialog.printer())
    
        def handlePreview(self):
            dialog = QtPrintSupport.QPrintPreviewDialog()
            dialog.paintRequested.connect(self.editor.print_)
            dialog.exec_()
    
        def handleTextChanged(self):
            enable = not self.editor.document().isEmpty()
            self.buttonPrint.setEnabled(enable)
            self.buttonPreview.setEnabled(enable)
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.resize(640, 480)
        window.show()
        sys.exit(app.exec_())
    

    To print-preview a graphics view, use its render method:

    def handlePreview(self):
        # dialog = QtPrintSupport.QPrintPreviewDialog() # PyQt5
        dialog = QtGui.QPrintPreviewDialog()
        dialog.paintRequested.connect(self.handlePaintRequest)
        dialog.exec_()
    
    def handlePaintRequest(self, printer):
        self.view.render(QtGui.QPainter(printer))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make preview button as usual form button next to submit button(as
I want to generate a thumbnail preview of videos in Java. I'm mostly JMF
I want to be able to preview an image for a user before they
I want to use an external browser window to implement a preview functionality in
want to know why String behaves like value type while using ==. String s1
Want the function to sort the table by HP but if duplicate HPs then
We have a WPF application that has a print preview dialog. When we create
I want to give users a preview of certain files on my site and
I want to give my clients the possibility to preview their personalized product. In
How can I avoid the Preview window displayed to a user using an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.