In this PyQt5 article i want to show you Creating Print Preview Dialog in PyQt5. basically we are using QPrintPreviewDialog class for this article. The QPrintPreviewDialog class provides a dialog for previewing and configuring page layouts for printer output.
What is QPrintPreviewDialog in PyQt5?
In PyQt5, QPrintPreviewDialog is a class that provides a dialog window for previewing and configuring the layout of pages before printing. It is part of the Qt printing framework and you can use that to display a preview of how the printed document will appear.
Key features and functionalities of QPrintPreviewDialog include:
- Previewing Print Output: It allows users to see a visual representation of the printed document, including text, graphics and formatting.
- Configuring Print Settings: Users can adjust different print settings such as page layout, orientation, paper size, margins and scaling directly inside the dialog window.
- Navigating Pages: Users can navigate through multiple pages of the document using navigation controls provided by the dialog, such as arrow buttons or page number input.
- Zooming: The dialog typically includes zoom controls that enable users to zoom in or out to inspect specific areas of the document more closely.
- Interactivity: Users can interact with the previewed document, such as selecting text or clicking hyperlinks, to simulate the actual printing experience accurately.
- Print Control: The dialog often includes options to initiate the printing process directly from the preview window, allowing users to print the document with the configured settings.
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports.
1 2 3 4 5 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit, QFontDialog, QColorDialog import sys from PyQt5.QtGui import QIcon from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog |
After that we create our Window class that extends from QMainWindow and we add the specific requirements and methods in that class. because we are using the codes from the previous article. so we need to just focus on print preview code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 QToolbar" self.top = 200 self.left = 500 self.width = 680 self.height = 480 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.createEditor() self.CreateMenu() self.show() def CreateMenu(self): mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('File') editMenu = mainMenu.addMenu('Edit') viewMenu = mainMenu.addMenu('View') helpMenu = mainMenu.addMenu('Help') printAction = QAction(QIcon("print.png"), "Print", self) printAction.triggered.connect(self.printDialog) fileMenu.addAction(printAction) printPreviewAction = QAction(QIcon("printprev.png"), "Print Preview", self) printPreviewAction.triggered.connect(self.printpreviewDialog) fileMenu.addAction(printPreviewAction) exiteAction = QAction(QIcon("exit.png"), 'Exit', self) exiteAction.setShortcut("Ctrl+E") exiteAction.triggered.connect(self.exitWindow) fileMenu.addAction(exiteAction) copyAction = QAction(QIcon("copy.png"), 'Copy', self) copyAction.setShortcut("Ctrl+C") editMenu.addAction(copyAction) saveAction = QAction(QIcon("Save.png"), 'Save', self) saveAction.setShortcut("Ctrl+S") editMenu.addAction(saveAction) pasteAction = QAction(QIcon("Paste.png"), 'Paste', self) pasteAction.setShortcut("Ctrl+P") editMenu.addAction(pasteAction) fontAction = QAction(QIcon("font.png"), "Font", self) fontAction.setShortcut("Ctrl+F") fontAction.triggered.connect(self.fontDialog) viewMenu.addAction(fontAction) colorAction = QAction(QIcon("color.png"), "Color", self) colorAction.triggered.connect(self.colorDialog) viewMenu.addAction(colorAction) self.toolbar = self.addToolBar('Toolbar') self.toolbar.addAction(copyAction) self.toolbar.addAction(saveAction) self.toolbar.addAction(pasteAction) self.toolbar.addAction(exiteAction) self.toolbar.addAction(fontAction) self.toolbar.addAction(colorAction) self.toolbar.addAction(printAction) self.toolbar.addAction(printPreviewAction) def exitWindow(self): self.close() def createEditor(self): self.textEdit = QTextEdit(self) self.setCentralWidget(self.textEdit) def fontDialog(self): font, ok = QFontDialog.getFont() if ok: self.textEdit.setFont(font) def colorDialog(self): color = QColorDialog.getColor() self.textEdit.setTextColor(color) def printDialog(self): printer = QPrinter(QPrinter.HighResolution) dialog = QPrintDialog(printer, self) if dialog.exec_() == QPrintDialog.Accepted: self.textEdit.print_(printer) |
After that we create two method in that class for our Print Preview Dialog so the first method is for Preview Dialog and the second method is for setting the QTextEdit to show that in Print Preview Dialog.
1 2 3 4 5 6 7 |
def printpreviewDialog(self): printer = QPrinter(QPrinter.HighResolution) previewDialog = QPrintPreviewDialog(printer, self) previewDialog.paintRequested.connect(self.printPreview) previewDialog.exec_() def printPreview(self, printer): self.textEdit.print_(printer) |
Also every PyQt5 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
So run the complete code and this will be the result.
This is the Complete source code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit, QFontDialog, QColorDialog import sys from PyQt5.QtGui import QIcon from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog class Window(QMainWindow): def __init__(self): super().__init__() # Set window properties self.title = "Codeloop - PyQt5 QToolbar" self.top = 200 self.left = 500 self.width = 680 self.height = 480 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Create the text editor and menu self.createEditor() self.CreateMenu() self.show() # Method to create the menu def CreateMenu(self): mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('File') editMenu = mainMenu.addMenu('Edit') viewMenu = mainMenu.addMenu('View') helpMenu = mainMenu.addMenu('Help') # Add actions to File menu printAction = QAction(QIcon("print.png"), "Print", self) printAction.triggered.connect(self.printDialog) fileMenu.addAction(printAction) printPreviewAction = QAction(QIcon("printprev.png"), "Print Preview", self) printPreviewAction.triggered.connect(self.printpreviewDialog) fileMenu.addAction(printPreviewAction) exiteAction = QAction(QIcon("exit.png"), 'Exit', self) exiteAction.setShortcut("Ctrl+E") exiteAction.triggered.connect(self.exitWindow) fileMenu.addAction(exiteAction) # Add actions to Edit menu copyAction = QAction(QIcon("copy.png"), 'Copy', self) copyAction.setShortcut("Ctrl+C") editMenu.addAction(copyAction) saveAction = QAction(QIcon("Save.png"), 'Save', self) saveAction.setShortcut("Ctrl+S") editMenu.addAction(saveAction) pasteAction = QAction(QIcon("Paste.png"), 'Paste', self) pasteAction.setShortcut("Ctrl+P") editMenu.addAction(pasteAction) # Add actions to View menu fontAction = QAction(QIcon("font.png"), "Font", self) fontAction.setShortcut("Ctrl+F") fontAction.triggered.connect(self.fontDialog) viewMenu.addAction(fontAction) colorAction = QAction(QIcon("color.png"), "Color", self) colorAction.triggered.connect(self.colorDialog) viewMenu.addAction(colorAction) # Add actions to the toolbar self.toolbar = self.addToolBar('Toolbar') self.toolbar.addAction(copyAction) self.toolbar.addAction(saveAction) self.toolbar.addAction(pasteAction) self.toolbar.addAction(exiteAction) self.toolbar.addAction(fontAction) self.toolbar.addAction(colorAction) self.toolbar.addAction(printAction) self.toolbar.addAction(printPreviewAction) # Method to exit the window def exitWindow(self): self.close() # Method to create the text editor def createEditor(self): self.textEdit = QTextEdit(self) self.setCentralWidget(self.textEdit) # Method to open font dialog and set font def fontDialog(self): font, ok = QFontDialog.getFont() if ok: self.textEdit.setFont(font) # Method to open color dialog and set text color def colorDialog(self): color = QColorDialog.getColor() self.textEdit.setTextColor(color) # Method to open print dialog and print text def printDialog(self): printer = QPrinter(QPrinter.HighResolution) dialog = QPrintDialog(printer, self) if dialog.exec_() == QPrintDialog.Accepted: self.textEdit.print_(printer) # Method to open print preview dialog def printpreviewDialog(self): printer = QPrinter(QPrinter.HighResolution) previewDialog = QPrintPreviewDialog(printer, self) previewDialog.paintRequested.connect(self.printPreview) previewDialog.exec_() # Method to handle printing in print preview dialog def printPreview(self, printer): self.textEdit.print_(printer) # Create a PyQt application instance App = QApplication(sys.argv) # Create an instance of Window class window = Window() # Execute the application sys.exit(App.exec()) |
FAQs:
What is PyQt5?
PyQt5 is a set of Python bindings for Qt application framework, and it was developed by Riverbank Computing. It allows you to create cross-platform desktop applications with Python using Qt toolkit. PyQt5 provides to different functionality of the underlying Qt framework, and you can use that for creation of powerful and feature-rich graphical user interfaces (GUIs). It includes modules for handling GUI elements, multimedia, network communication, database integration and more.
Subscribe and Get Free Video Courses & Articles in your Email