Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtprintsupport-index.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qtprintsupport-index.html
6 \title Qt Print Support
7 \brief A guide to producing printed output with Qt's paint system and widgets.
8 \ingroup qt-graphics
9
10
11 The Qt Print Support module provides extensive cross-platform support for
12 printing. Using the printing systems on each platform, Qt applications can
13 print to attached printers and across networks to remote printers. The
14 printing system also supports PDF file generation, providing the foundation
15 for basic report generation facilities.
16
17 Qt Print Support is not available on iOS.
18
19 \tableofcontents
20
21 \section1 Classes Supporting Printing
22
23 The following classes support the selecting and setting up of printers and
24 printing output.
25
26 \annotatedlist printing
27
28 \section1 Paint Devices and Printing
29
30 Printers are represented by QPrinter, a paint device that provides
31 functionality specific to printing, such as support for multiple pages and
32 double-sided output. As a result, printing involves using a QPainter to paint
33 onto a series of pages in the same way that you would paint onto a custom
34 widget or image.
35
36 \section2 Creating a QPrinter
37
38 Although QPrinter objects can be constructed and set up without requiring user
39 input, printing is often performed as a result of a request by the user;
40 for example, when the user selects the \uicontrol{File|Print...} menu item in
41 a GUI application. In such cases, a newly-constructed QPrinter object is
42 supplied to a QPrintDialog, allowing the user to specify the printer to use,
43 paper size, and other printing properties.
44
45 \snippet widgetprinting.cpp 1
46
47 It is also possible to set certain default properties by modifying the
48 QPrinter before it is supplied to the print dialog. For example, applications
49 that generate batches of reports for printing may set up the QPrinter to
50 \l{QPrinter::setOutputFileName()}{write to a local file} by default rather
51 than to a printer.
52
53 \section2 Painting onto a Page
54
55 Once a QPrinter object has been constructed and set up, a QPainter can be used
56 to perform painting operations on it. We can construct and set up a painter in
57 the following way:
58
59 \snippet printing-qprinter/object.cpp 0
60
61 Since the QPrinter starts with a blank page, we only need to call the
62 \l{QPrinter::}{newPage()} function after drawing each page, except for the
63 last page.
64
65 The document is sent to the printer, or written to a local file, when we call
66 \l{QPainter::}{end()}.
67
68 \section2 Coordinate Systems
69
70 QPrinter provides functions that can be used to obtain information about the
71 dimensions of the paper (the paper rectangle) and the dimensions of the
72 printable area (the page rectangle). These are given in logical device
73 coordinates that may differ from the physical coordinates used by the device
74 itself, indicating that the printer is able to render text and graphics at a
75 (typically higher) resolution than the user's display.
76
77 Although we do not need to handle the conversion between logical and physical
78 coordinates ourselves, we still need to apply transformations to painting
79 operations because the pixel measurements used to draw on screen are often
80 too small for the higher resolutions of typical printers.
81
82 \table
83 \row \li \b{Printer and Painter Coordinate Systems}
84
85 The \l{QPrinter::}{paperRect()} and \l{QPrinter::}{pageRect()} functions
86 provide information about the size of the paper used for printing and the
87 area on it that can be painted on.
88
89 The rectangle returned by \l{QPrinter::}{pageRect()} usually lies inside
90 the rectangle returned by \l{QPrinter::}{paperRect()}. You do not need to
91 take the positions and sizes of these area into account when using a QPainter
92 with a QPrinter as the underlying paint device; the origin of the painter's
93 coordinate system will coincide with the top-left corner of the page
94 rectangle, and painting operations will be clipped to the bounds of the
95 drawable part of the page.
96
97 \li \inlineimage printer-rects.png
98 \endtable
99
100 The paint system automatically uses the correct device metrics when painting
101 text but, if you need to position text using information obtained from
102 font metrics, you need to ensure that the print device is specified when
103 you construct QFontMetrics and QFontMetricsF objects, or ensure that each
104 QFont used is constructed using the form of the constructor that accepts a
105 QPaintDevice argument.
106
107 \section1 Printing Widgets
108
109 To print a widget, you can use the QWidget::render() function. As mentioned,
110 the printer's resolution is usually higher than the screen resolution, so you
111 will have to scale the painter. You may also want to position the widget on
112 the page. The following code sample shows how this may look.
113
114 \snippet widgetprinting.cpp 0
115
116 This will center the widget on the page and scale it so that it fits the page.
117
118 \section1 Printing from Complex Widgets
119
120 Certain widgets, such as QTextEdit and QGraphicsView, display rich content
121 that is typically managed by instances of other classes, such as QTextDocument
122 and QGraphicsScene. As a result, it is these content handling classes that
123 usually provide printing functionality, either via a function that can be used
124 to perform the complete task, or via a function that accepts an existing
125 QPainter object. Some widgets provide convenience functions to expose
126 underlying printing features, avoiding the need to obtain the content handler
127 just to call a single function.
128
129 The following table shows which class and function are responsible for
130 printing from a selection of different widgets. For widgets that do not expose
131 printing functionality directly, the content handling classes containing this
132 functionality can be obtained via a function in the corresponding widget's API.
133
134 \table
135 \header \li Widget \li Printing function \li Accepts
136 \row \li QGraphicsView \li QGraphicsView::render() \li QPainter
137 \row \li QSvgWidget \li QSvgRenderer::render() \li QPainter
138 \row \li QTextEdit \li QTextDocument::print() \li QPrinter
139 \row \li QTextLayout \li QTextLayout::draw() \li QPainter
140 \row \li QTextLine \li QTextLine::draw() \li QPainter
141 \endtable
142
143 QTextEdit requires a QPrinter rather than a QPainter because it uses
144 information about the configured page dimensions in order to insert page
145 breaks at the most appropriate places in printed documents.
146
147 \include module-use.qdocinc using qt module
148 \snippet snippets/CMakeLists.txt cmake_use
149
150 See also the \l {Build with CMake} overview.
151
152 \include module-use.qdocinc building with qmake
153 \snippet snippets.pro qmake_use
154
155 \section1 Module Evolution
156 \l{Changes to Qt Print Support} lists important changes in the module API
157 and functionality that were done for the Qt 6 series of Qt.
158
159 \section1 Licenses and Trademarks
160
161 The Qt Print Support module is available under commercial licenses from
162 \l{The Qt Company}.
163 In addition, it is available under free software licenses:
164 The \l{GNU Lesser General Public License, version 3}, or
165 the \l{GNU General Public License, version 2}.
166 See \l{Qt Licensing} for further details.
167
168 Please note that Adobe\reg places restrictions on the use of its trademarks
169 (including logos) in conjunction with PDF; e.g. "Adobe PDF". Please refer
170 to \l{http://www.adobe.com}{www.adobe.com} for guidelines.
171*/
172
173/*!
174 \page pdf-licensing.html
175 \title Notes about PDF Licensing
176 \ingroup licensing
177 \brief Details of restrictions on the use of PDF-related trademarks.
178
179 Please note that Adobe\reg places restrictions on the use of its trademarks
180 (including logos) in conjunction with PDF; e.g. "Adobe PDF". Please refer
181 to \l{http://www.adobe.com}{www.adobe.com} for guidelines.
182*/
183
184/*!
185 \group printing
186 \title Printer and Printing APIs
187 \brief Classes for producing printed output
188 \ingroup groups
189*/