PyQt5 Tutorials
About Lesson

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.

PyQt5 Print Dialog
PyQt5 Print Dialog

 

 

 

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.

 

 

 

And this is the converted code also we have added our clicked() signals with the methods that we want to connect.

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.

 

 

 

And these methods are for creating PrintPreveiw Dialog.

 

 

 

Run the complete code and this is the result.

PyQt5 PrinterDialog - Working with QPrinterDialog in PyQt
PyQt5 PrinterDialog – Working with QPrinterDialog in PyQt

 

 

 

And this is the PrintPreview dialog.

Working with QPrinterDialog in PyQt
Working with QPrinterDialog in PyQt