Professional Documents
Culture Documents
Document 1
Document 1
Printing with Qt
Qt provides extensive cross-platform support for printing. Using the printing systems on each platform, Qt applications can print to attached
printers and across networks to remote printers. Qt's printing system also enables PostScript and PDF files to be generated, providing the
foundation for basic report generation facilities.
QPrintEngine Defines an interface for how QPrinter interacts with a given printing subsystem
QPrintPreviewDialog Dialog for previewing and configuring page layouts for printer output
Creating a QPrinter
Although QPrinter objects can be constructed and set up without requiring user input, printing is often performed as a result of a request by the
user; for example, when the user selects the File|Print... menu item in a GUI application. In such cases, a newly-constructed QPrinter object is
supplied to a QPrintDialog, allowing the user to specify the printer to use, paper size, and other printing properties.
QPrinter printer;
It is also possible to set certain default properties by modifying the QPrinter before it is supplied to the print dialog. For example, applications
that generate batches of reports for printing may set up the QPrinter to write to a local file by default rather than to a printer.
painter.end();
Since the QPrinter starts with a blank page, we only need to call the newPage() function after drawing each page, except for the last page.
The document is sent to the printer, or written to a local file, when we call end().
Coordinate Systems
QPrinter provides functions that can be used to obtain information about the dimensions of the paper (the paper rectangle) and the dimensions
of the printable area (the page rectangle). These are given in logical device coordinates that may differ from the physical coordinates used by
the device itself, indicating that the printer is able to render text and graphics at a (typically higher) resolution than the user's display.
Although we do not need to handle the conversion between logical and physical coordinates ourselves, we still need to apply transformations
to painting operations because the pixel measurements used to draw on screen are often too small for the higher resolutions of typical printers.
The paint system automatically uses the correct device metrics when painting text but, if you need to position text using information obtained
from font metrics, you need to ensure that the print device is specified when you construct QFontMetrics and QFontMetricsF objects, or ensure
that each QFont used is constructed using the form of the constructor that accepts a QPaintDevice argument.
Printing Widgets
To print a widget, you can use the QWidget::render() function. As mentioned, the printer's resolution is usually higher than the screen resolution,
so you will have to scale the painter. You may also want to position the widget on the page. The following code sample shows how this may
look.
QPainter painter;
painter.begin(&printer);
double xscale = printer.pageRect().width()/double(myWidget->width());
double yscale = printer.pageRect().height()/double(myWidget->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);
myWidget->render(&painter);
This will center the widget on the page and scale it so that it fits the page.
The following table shows which class and function are responsible for printing from a selection of different widgets. For widgets that do not
expose printing functionality directly, the content handling classes containing this functionality can be obtained via a function in the
corresponding widget's API.
QTextEdit requires a QPrinter rather than a QPainter because it uses information about the configured page dimensions in order to insert
page breaks at the most appropriate places in printed documents.
[Previous: Styling]