Every developer knows how unexpectedly complex printing becomes when layouts “float,” fonts look different, and the same code behaves differently on different platforms. Have you experienced the feeling that printing is a separate world with its own pitfalls?
This chapter gradually reveals why printing in Qt is actually simpler than it seems, and where exactly the key idea lies that removes up to 80% of typical problems. Here you’ll discover how a unified drawing mechanism eliminates logic duplication.
It will show how the same code works for screen, printer, and PDF, which 5 key settings actually affect the result, and how to correctly manage pages, orientation, and scaling—without “magic numbers” and hacky solutions.
After this chapter, your approach to printing changes forever: from chaotic experimentation—to controlled and predictable results. Skip it—and you’ll continue solving already-solved problems.
This chapter includes ready-to-use code examples.
Chapter Self-Check
Why is the QPrinter class inherited from QPaintDevice and how does this simplify the developer’s task?Answer
Correct answer: Thanks to inheritance from QPaintDevice, printing works like regular drawing—the same QPainter methods used for screen output are used for printer output, eliminating the need to learn a separate API for printing.
Why is it necessary to explicitly connect the QtPrintSupport module in the project file?Answer
Correct answer: Printing classes (QPrinter, QPrintDialog) are in a separate QtPrintSupport module, which is not included by default, so it must be added via QT += printsupport or target_link_libraries in CMake.
What happens if you pass an empty string to the setOutputFileName() method?Answer
Correct answer: The system will ignore output redirection to file, and printing will be performed to a physical printing device instead of saving to a file.
How do you redirect print output to a PDF file instead of sending to a printer?Answer
Correct answer: Call the setOutputFormat() method with the value QPrinter::PdfFormat and specify the output file name via setOutputFileName()—this will create a PDF document instead of sending to a physical printer.
Why is it necessary to call the viewport() method before starting drawing when printing?Answer
Correct answer: Most printers cannot use the entire sheet area for printing, so viewport() returns the available printable rectangular area, allowing you to avoid output outside the printable zone.
What is the purpose of the setFontEmbeddingEnabled() method and when should it be used?Answer
Correct answer: The method embeds fonts into the document, ensuring the file contains all necessary fonts; this is useful when using files on other platforms where the required fonts may not be installed.
What combination of methods should be used to set landscape orientation and print three copies?Answer
Correct answer: setPageOrientation(QPageLayout::Landscape) for landscape orientation and setCopyCount(3) to set the number of copies.
Why is std::unique_ptr used for the QPrinter object in the example code?Answer
Correct answer: This follows the RAII principle and automatically frees memory when the object is destroyed, eliminating the need for explicit deletion in the destructor and reducing the risk of memory leaks.
Why pass a pointer to the parent widget in the second parameter of the QPrintDialog constructor?Answer
Correct answer: This allows the dialog window to be centered relative to the main application window, providing better user navigation.
How do you calculate the number of pages to print after the user selects a range in the print dialog?Answer
Correct answer: Subtract fromPage() from toPage() and add one: int nPages = dlg.toPage() – dlg.fromPage() + 1.
What happens if you forget to call newPage() when printing a multi-page document?Answer
Correct answer: All content will be drawn on one page on top of each other, since newPage() is necessary to create a new page in the print job before continuing to draw.
Which three key methods of the QPrinter class allow managing color mode, page size, and page range?Answer
Correct answer: setColorMode() for managing color/black-and-white mode, setPageSize() for changing page size via QPageSize object, and setFromTo() for setting the range of pages to print.
Practical Exercises
Easy Level
Business Card Printing Application
Create a Qt application with a “Print Business Card” button that prints a simple business card with your name, title, and contact information. Use different font sizes and colors for text formatting. Add a decorative border around the card.
Hints: Connect the QtPrintSupport module in the project file. Create a QPrinter and use QPainter to draw on it. Use the viewport() method to get the printable area. Apply drawRect() for the border, setFont() for different text sizes, and drawText() with alignment flags. Open the print dialog via QPrintDialog before output.
Medium Level
Report Generator with Output Format Selection
Develop an application that creates a simple report with a header, creation date, data table (3-4 rows), and totals. The user should be able to choose where to output the report: to a printer or save as a PDF file. Implement mode switcher (radio buttons) and programmatic page orientation setting to landscape mode for more convenient table placement.
Hints: Use QRadioButton to select output mode. When selecting PDF, use setOutputFormat(QPrinter::PdfFormat) and setOutputFileName(). For landscape orientation, apply setPageOrientation(QPageLayout::Landscape). Draw the table using drawLine() and drawText(). Create a separate method for drawing the report that can be used for both printing and PDF.
Hard Level
Multi-page Photo Album with Layout
Create an application for printing a photo album: the user selects multiple images (minimum 6), and the program automatically places 2 images on each page with captions below them. The first page should have a cover with the album title and creation date. Implement image scaling while preserving proportions, add page numbering in the footer. Provide the option to print in color or black-and-white mode.
Hints: Use QFileDialog to select multiple images. Load them via QImage. Organize a page loop with newPage() call for each new page after the first. Scale images using the scaled() method with the Qt::KeepAspectRatio flag. Calculate positions for placing two images on a page (upper and lower halves considering margins). For the cover, use large centered font. For page numbering, use a counter in the loop and drawText() in the corner of the page. Add QCheckBox for color/black-and-white mode selection and apply setColorMode().
💬 Join the Discussion!
Figured out printing in Qt? Have questions about configuring QPrinter or organizing multi-page printing?
Share your experience with PDF generation, talk about non-standard printing tasks, or help other readers master the chapter material!