In this Python PyQt5 lesson we want to learn about Python PyQt5 QPrinterDialog, also we will learn that how you can create print preview dialog, so the the QPrintDialog class provides a dialog for making the printer’s configuration. using this dialog users can change document-related settings, such as the paper size and orientation, type of print (color or grayscale), range of pages, and number of copies to print, and the QPrintPreviewDialogclass provides a dialog for previewing and configuring page layouts for printer output.
Now open your Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Widget window. now we add widgets in Qt Designer.
- Add a vertical layout at the top
- Add a horizontal layout at the bottom
- In the VBoxLayout add a QTextEdit
- In the HBoxLayout add two QPushButton
So this is the design.
After completing the design you need to save the .ui file, iam going to name it printd.ui, now copy the file and paste it in the Scripts folder of your Python installation, because we want to convert our ui file in to python file and for converting you need to use pyuic5 module. pyuic5 module is located in the Scripts folder of your Python installation, run this command for converting in your terminal.
1 |
pyuic5 printd.ui -o printdialog.py -x |
And this is the converted code also we have added our clicked() signals with the methods that we want to connect.
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 |
# -*- coding: utf-8 -*- # Form implementation generated from # reading ui file 'printd.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this file # will be lost when pyuic5 is # run again. Do not edit this file unless you know # what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QWidget from PyQt5.QtPrintSupport import QPrinter, QPrintDialog,\ QPrintPreviewDialog class Ui_Form(QWidget): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(445, 358) self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form) self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.textEdit = QtWidgets.QTextEdit(Form) self.textEdit.setObjectName("textEdit") self.verticalLayout.addWidget(self.textEdit) self.verticalLayout_2.addLayout(self.verticalLayout) self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setObjectName("pushButton") #connected the print dialog self.pushButton.clicked.connect(self.print_file) self.horizontalLayout.addWidget(self.pushButton) self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setObjectName("pushButton_2") #connected the print preview dialog self.pushButton_2.clicked.connect(self.print_preview_dialog) self.horizontalLayout.addWidget(self.pushButton_2) self.verticalLayout_2.addLayout(self.horizontalLayout) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) #print dialog method def print_file(self): printer = QPrinter(QPrinter.HighResolution) dialog = QPrintDialog(printer, self) if dialog.exec_() == QPrintDialog.Accepted: self.textEdit.print_(printer) #these methods are for print preview dialog def print_preview_dialog(self): printer = QPrinter(QPrinter.HighResolution) previewDialog = QPrintPreviewDialog(printer, self) previewDialog.paintRequested.connect(self.print_preview) previewDialog.exec_() def print_preview(self, printer): self.textEdit.print_(printer) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "Print File")) self.pushButton_2.setText(_translate("Form", "Print Preview")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_()) |
You can see that we have added our two methods, we have already connected these methods with the clicked signal of the QPushButton.
This method is for creating of the print dialog. we have created the object of the QPrinter, also we have created the object of QPrintDialog and after that we have executed our dialog.
1 2 3 4 5 6 7 |
def print_file(self): printer = QPrinter(QPrinter.HighResolution) dialog = QPrintDialog(printer, self) if dialog.exec_() == QPrintDialog.Accepted: self.textEdit.print_(printer) |
And these methods are for creating PrintPreveiw Dialog.
1 2 3 4 5 6 7 8 9 10 |
def print_preview_dialog(self): printer = QPrinter(QPrinter.HighResolution) previewDialog = QPrintPreviewDialog(printer, self) previewDialog.paintRequested.connect(self.print_preview) previewDialog.exec_() def print_preview(self, printer): self.textEdit.print_(printer) |
Run the complete code and this is the result.
And this is the PrintPreview dialog.