1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the documentation of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
30 \title Printer and Printing APIs
31 \brief Classes for producing printed output
37 \title Printing with Qt
41 \contentspage The Paint System
43 \brief A guide to producing printed output with Qt's paint system and widgets.
45 Qt provides extensive cross-platform support for printing. Using the printing
46 systems on each platform, Qt applications can print to attached printers and
47 across networks to remote printers. Qt's printing system also enables PostScript
48 and PDF files to be generated, providing the foundation for basic report
49 generation facilities.
53 \section1 Classes Supporting Printing
55 The following classes support the selecting and setting up of printers and
58 \annotatedlist printing
60 \section1 Paint Devices and Printing
62 In Qt, printers are represented by QPrinter, a paint device that provides
63 functionality specific to printing, such as support for multiple pages and
64 double-sided output. As a result, printing involves using a QPainter to paint
65 onto a series of pages in the same way that you would paint onto a custom
68 \section2 Creating a QPrinter
70 Although QPrinter objects can be constructed and set up without requiring user
71 input, printing is often performed as a result of a request by the user;
72 for example, when the user selects the \gui{File|Print...} menu item in a GUI
73 application. In such cases, a newly-constructed QPrinter object is supplied to
74 a QPrintDialog, allowing the user to specify the printer to use, paper size, and
75 other printing properties.
77 \snippet examples/richtext/orderform/mainwindow.cpp 18
79 It is also possible to set certain default properties by modifying the QPrinter
80 before it is supplied to the print dialog. For example, applications that
81 generate batches of reports for printing may set up the QPrinter to
82 \l{QPrinter::setOutputFileName()}{write to a local file} by default rather than
85 \section2 Painting onto a Page
87 Once a QPrinter object has been constructed and set up, a QPainter can be used
88 to perform painting operations on it. We can construct and set up a painter in
91 \snippet doc/src/snippets/printing-qprinter/object.cpp 0
93 Since the QPrinter starts with a blank page, we only need to call the
94 \l{QPrinter::}{newPage()} function after drawing each page, except for the
97 The document is sent to the printer, or written to a local file, when we call
98 \l{QPainter::}{end()}.
100 \section2 Coordinate Systems
102 QPrinter provides functions that can be used to obtain information about the
103 dimensions of the paper (the paper rectangle) and the dimensions of the
104 printable area (the page rectangle). These are given in logical device
105 coordinates that may differ from the physical coordinates used by the device
106 itself, indicating that the printer is able to render text and graphics at a
107 (typically higher) resolution than the user's display.
109 Although we do not need to handle the conversion between logical and physical
110 coordinates ourselves, we still need to apply transformations to painting
111 operations because the pixel measurements used to draw on screen are often
112 too small for the higher resolutions of typical printers.
115 \row \o \bold{Printer and Painter Coordinate Systems}
117 The \l{QPrinter::}{paperRect()} and \l{QPrinter::}{pageRect()} functions
118 provide information about the size of the paper used for printing and the
119 area on it that can be painted on.
121 The rectangle returned by \l{QPrinter::}{pageRect()} usually lies inside
122 the rectangle returned by \l{QPrinter::}{paperRect()}. You do not need to
123 take the positions and sizes of these area into account when using a QPainter
124 with a QPrinter as the underlying paint device; the origin of the painter's
125 coordinate system will coincide with the top-left corner of the page
126 rectangle, and painting operations will be clipped to the bounds of the
127 drawable part of the page.
129 \o \inlineimage printer-rects.png
132 The paint system automatically uses the correct device metrics when painting
133 text but, if you need to position text using information obtained from
134 font metrics, you need to ensure that the print device is specified when
135 you construct QFontMetrics and QFontMetricsF objects, or ensure that each QFont
136 used is constructed using the form of the constructor that accepts a
137 QPaintDevice argument.
139 \section1 Printing Widgets
141 To print a widget, you can use the QWidget::render() function. As mentioned,
142 the printer's resolution is usually higher than the screen resolution, so you
143 will have to scale the painter. You may also want to position the widget on the
144 page. The following code sample shows how this may look.
146 \snippet doc/src/snippets/widgetprinting.cpp 0
148 This will center the widget on the page and scale it so that it fits the page.
150 \section1 Printing from Complex Widgets
152 Certain widgets, such as QTextEdit and QGraphicsView, display rich content
153 that is typically managed by instances of other classes, such as QTextDocument
154 and QGraphicsScene. As a result, it is these content handling classes that
155 usually provide printing functionality, either via a function that can be used
156 to perform the complete task, or via a function that accepts an existing
157 QPainter object. Some widgets provide convenience functions to expose underlying
158 printing features, avoiding the need to obtain the content handler just to call
161 The following table shows which class and function are responsible for
162 printing from a selection of different widgets. For widgets that do not expose
163 printing functionality directly, the content handling classes containing this
164 functionality can be obtained via a function in the corresponding widget's API.
167 \header \o Widget \o Printing function \o Accepts
168 \row \o QGraphicsView \o QGraphicsView::render() \o QPainter
169 \row \o QSvgWidget \o QSvgRenderer::render() \o QPainter
170 \row \o QTextEdit \o QTextDocument::print() \o QPrinter
171 \row \o QTextLayout \o QTextLayout::draw() \o QPainter
172 \row \o QTextLine \o QTextLine::draw() \o QPainter
175 QTextEdit requires a QPrinter rather than a QPainter because it uses information
176 about the configured page dimensions in order to insert page breaks at the most
177 appropriate places in printed documents.
181 \page pdf-licensing.html
182 \title Notes about PDF Licensing
184 \brief Details of restrictions on the use of PDF-related trademarks.
186 Please note that Adobe\reg places restrictions on the use of its trademarks
187 (including logos) in conjunction with PDF; e.g. "Adobe PDF". Please refer
188 to \l{http://www.adobe.com}{www.adobe.com} for guidelines.