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
100 \endtable
101
102 The paint system automatically uses the correct device metrics when painting
103 text but, if you need to position text using information obtained from
104 font metrics, you need to ensure that the print device is specified when
105 you construct QFontMetrics and QFontMetricsF objects, or ensure that each
106 QFont used is constructed using the form of the constructor that accepts a
107 QPaintDevice argument.
108
109 \section1 Printing Widgets
110
111 To print a widget, you can use the QWidget::render() function. As mentioned,
112 the printer's resolution is usually higher than the screen resolution, so you
113 will have to scale the painter. You may also want to position the widget on
114 the page. The following code sample shows how this may look.
115
116 \snippet widgetprinting.cpp 0
117
118 This will center the widget on the page and scale it so that it fits the page.
119
120 \section1 Printing from Complex Widgets
121
122 Certain widgets, such as QTextEdit and QGraphicsView, display rich content
123 that is typically managed by instances of other classes, such as QTextDocument
124 and QGraphicsScene. As a result, it is these content handling classes that
125 usually provide printing functionality, either via a function that can be used
126 to perform the complete task, or via a function that accepts an existing
127 QPainter object. Some widgets provide convenience functions to expose
128 underlying printing features, avoiding the need to obtain the content handler
129 just to call a single function.
130
131 The following table shows which class and function are responsible for
132 printing from a selection of different widgets. For widgets that do not expose
133 printing functionality directly, the content handling classes containing this
134 functionality can be obtained via a function in the corresponding widget's API.
135
136 \table
137 \header \li Widget \li Printing function \li Accepts
138 \row \li QGraphicsView \li QGraphicsView::render() \li QPainter
139 \row \li QSvgWidget \li QSvgRenderer::render() \li QPainter
140 \row \li QTextEdit \li QTextDocument::print() \li QPrinter
141 \row \li QTextLayout \li QTextLayout::draw() \li QPainter
142 \row \li QTextLine \li QTextLine::draw() \li QPainter
143 \endtable
144
145 QTextEdit requires a QPrinter rather than a QPainter because it uses
146 information about the configured page dimensions in order to insert page
147 breaks at the most appropriate places in printed documents.
148
149 \include module-use.qdocinc using qt module
150 \snippet snippets/CMakeLists.txt cmake_use
151
152 See also the \l {Build with CMake} overview.
153
154 \include module-use.qdocinc building with qmake
155 \snippet snippets.pro qmake_use
156
157 \section1 Module Evolution
158 \l{Changes to Qt Print Support} lists important changes in the module API
159 and functionality that were done for the Qt 6 series of Qt.
160
161 \section1 Licenses and Trademarks
162
163 The Qt Print Support module is available under commercial licenses from
164 \l{The Qt Company}.
165 In addition, it is available under free software licenses:
166 The \l{GNU Lesser General Public License, version 3}, or
167 the \l{GNU General Public License, version 2}.
168 See \l{Qt Licensing} for further details.
169
170 Please note that Adobe\reg places restrictions on the use of its trademarks
171 (including logos) in conjunction with PDF; e.g. "Adobe PDF". Please refer
172 to \l{http://www.adobe.com}{www.adobe.com} for guidelines.
173*/
174
175/*!
176 \page pdf-licensing.html
177 \title Notes about PDF Licensing
178 \ingroup licensing
179 \brief Details of restrictions on the use of PDF-related trademarks.
180
181 Please note that Adobe\reg places restrictions on the use of its trademarks
182 (including logos) in conjunction with PDF; e.g. "Adobe PDF". Please refer
183 to \l{http://www.adobe.com}{www.adobe.com} for guidelines.
184*/
185
186/*!
187 \group printing
188 \title Printer and Printing APIs
189 \brief Classes for producing printed output
190 \ingroup groups
191*/