[파이썬] PyQt `QPrintDialog`와 인쇄 관리

PyQt is a popular Python binding for the Qt application framework, which allows developers to create powerful and intuitive graphical user interfaces. One of the common tasks in creating GUI applications is handling printing functionality. In this blog post, we will explore how to use the PyQt QPrintDialog class to manage printing in our Python applications.

The QPrintDialog Class

The QPrintDialog class is a pre-defined dialog class provided by PyQt that allows users to select printing options for a document. It provides a standard user interface that includes options like printer selection, page range, and number of copies.

To use QPrintDialog, we first need to import the required classes from the PyQt library:

from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QPrintDialog

Next, we set up a basic PyQt application window with some placeholder content:

app = QApplication([])
window = QMainWindow()
text_edit = QTextEdit()

window.setCentralWidget(text_edit)
window.show()
app.exec_()

Once we have our application window set up, we can create an instance of QPrintDialog:

print_dialog = QPrintDialog()

Executing the Print Dialog

The QPrintDialog class provides a method called exec_() that allows us to execute the dialog and capture the user’s input. We can use this method to open the print dialog and fetch the chosen printing options:

if print_dialog.exec_() == QPrintDialog.Accepted:
    # User clicked on Print button
    printer = print_dialog.printer()
    # Retrieve the selected printer
    print_options = print_dialog.printerOptions()
    # Retrieve the selected printing options

    # Code to handle printing goes here

In the code above, we check if the user clicked on the “Print” button by comparing the return value of the exec_() method with QPrintDialog.Accepted. If the condition is satisfied, we retrieve the selected printer and printing options using the respective methods provided by the QPrintDialog class.

Printing with QPrinter

To actually perform the printing, we need to work with the QPrinter class. This class represents the printer and provides methods to configure various printing properties. Here’s an example of how to configure the QPrinter object using the printer and options obtained from the QPrintDialog:

printer.setPrinterName(printer.printerName())
# Set the printer name
printer.setPageLayout(print_options.pageLayout())
# Set the page layout
printer.setPrintRange(print_options.printRange())
# Set the print range
printer.setNumCopies(print_options.numCopies())
# Set the number of copies

# Additional configuration options can be set here

Once the QPrinter object is configured, we can use it to print our content. Here’s an example of how to print the contents of a QTextEdit widget:

text_edit.print_(printer)

In the code above, the print_() method of the QTextEdit class is called with the QPrinter object as an argument.

Conclusion

The PyQt QPrintDialog class provides a convenient way to manage printing functionality in our Python applications. By using this class, we can allow users to choose printing options, such as printer selection and page range, and easily perform printing tasks. With PyQt and QPrintDialog, creating powerful and customizable printing features in our applications becomes effortless.