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