CopperSpice API  1.7.2
Printer Support

A guide to producing printed output with the paint system and widgets. Extensive cross platform support for printing is available in CopperSpice. Using the printing systems on each platform an application can print to local printers or network connected printers. The printing system also supports PDF file generation, providing the foundation for basic report generation facilities.

Classes Supporting Printing

Refer to Printing Classes for a list of classes which support printer configuration and printing output.

Paint Devices and Printing

Printers are represented by QPrinter which is a paint device that provides functionality specific to printing, such as support for multiple pages and double-sided output. As a result, printing involves using a QPainter to paint onto a series of pages in the same way that you would paint onto a custom widget or image.

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 a menu item such as the File, Print selection in a GUI application. Normally, a constructed QPrinter object will be supplied to a QPrintDialog, allowing the user to specify the printer, paper size, and other printing properties.

QPrinter printer;
QPrintDialog dialog(&printer, this);
dialog.setWindowTitle(tr("Print Document"));
if (editor->textCursor().hasSelection()) {
dialog.addEnabledOption(QAbstractPrintDialog::PrintSelection);
}
if (dialog.exec() != QDialog::Accepted) {
return;
}

It is also possible to set certain default properties by modifying QPrinter before it is supplied to the print dialog. For example, applications that generate batches of reports for printing may set up QPrinter::setOutputFileName() by default rather than to a printer.

Painting on a Page

Once a QPrinter object has been constructed and set up QPainter can be used to perform painting operations on it.

Since the QPrinter starts with a blank page, we only need to call the QPrinter::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 QPainter::end().

QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("print.ps");
QPainter painter;
painter.begin(&printer);
for (int page = 0; page < numberOfPages; ++page) {
// Use the painter to draw on the page
if (page != lastPage) {
printer.newPage();
}
}
painter.end();

Coordinate Systems

QPrinter provides methods 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 QPrinter::paperRect() and QPrinter::pageRect() methods provide information about the size of the paper used for printing and the area on it that can be painted on.

The rectangle returned by QPrinter::pageRect() usually lies inside the rectangle returned by QPrinter::paperRect(). You do not need to take the positions and sizes of these area into account when using a QPainter with a QPrinter as the underlying paint device; the origin of the painter's coordinate system will coincide with the top-left corner of the page rectangle, and painting operations will be clipped to the bounds of the drawable part of the page.

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 use the QWidget::render() method. 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 can be done.

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.

Printing from Complex Widgets

Certain widgets such as QTextEdit and QGraphicsView, display rich content that is typically managed by instances of other classes, such as QTextDocument and QGraphicsScene. As a result, it is these content handling classes that usually provide printing functionality, either via a function that can be used to perform the complete task, or via a function that accepts an existing QPainter object. Some widgets provide convenience functions to expose underlying printing features, avoiding the need to obtain the content handler just to call a single function.

The following table shows which class and method 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.

Widget Method
QGraphicsView QGraphicsView::render()
QSvgWidget QSvgRenderer::render()
QTextEdit QTextDocument::print()
QTextLayout QTextLayout::draw()
QTextLine QTextLine::draw()