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
paintsystem.qdoc
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \group painting
6 \title Painting Classes
7 \ingroup groups
8
9 \brief Classes that provide support for painting.
10
11 See also this introduction to the \l{coordsys.html}{Qt coordinate system.}
12*/
13
14/*!
15 \group painting-3D
16 \title Rendering in 3D
17 \ingroup groups
18
19 \brief Classes that provide support for rendering in 3D.
20*/
21
22/*!
23 \page paintsystem.html
24 \title Paint System
25 \brief A system for painting on the screen or on print devices using the same API
26 \ingroup qt-graphics
27 \ingroup frameworks-technologies
28 \ingroup qt-basic-concepts
29
30
31 Qt's paint system provides a unified and flexible framework for
32 rendering graphics on various surfaces, such as on-screen widgets,
33 images, and printed pages. It is primarily built around the QPainter,
34 QPaintDevice, and QPaintEngine classes.
35
36 QPainter is used to perform drawing operations, QPaintDevice is an
37 abstraction of a two-dimensional space that can be painted on
38 using a QPainter, and QPaintEngine provides the interface that the
39 painter uses to draw onto different types of devices. The
40 QPaintEngine class is used internally by QPainter and
41 QPaintDevice, and is hidden from application programmers unless
42 they create their own device type.
43
44 \image paintsystem-core.png {Illustration showing the relations of
45 the paint system}
46
47 The main benefit of this approach is that all painting follows the
48 same painting pipeline making it easy to add support for new
49 features and providing default implementations for unsupported
50 ones.
51
52 \section1 Topics
53 \list
54 \li \l{Classes for Painting}
55 \li \l{Paint Devices and Backends}
56 \li \l{Drawing and Filling}
57 \li \l{Coordinate System}
58 \li \l{Reading and Writing Image Files}
59 \endlist
60
61 \section1 Classes for Painting
62
63 These classes provide support for painting onto a paint device.
64
65 \annotatedlist painting
66
67*/
68
69
70/*!
71 \page paintsystem-devices.html
72 \title Paint Devices and Backends
73
74 \nextpage Drawing and Filling
75
76 \section1 Creating a Paint Device
77
78 The QPaintDevice class is the base class of objects that can be
79 painted, i.e. QPainter can draw on any QPaintDevice
80 subclass. QPaintDevice's drawing capabilities are among others
81 implemented by QWidget, QImage, QPixmap, QPicture, QPrinter, and
82 QOpenGLPaintDevice.
83
84 \table 100%
85
86 \row \li \b Widget
87
88 The QWidget class is the base class of user interface
89 elements in the \l {Qt Widgets} module. It receives mouse, keyboard
90 and other events from the window system, and paints a
91 representation of itself on the screen.
92
93 \row \li \b Image
94
95 The QImage class provides a hardware-independent image
96 representation which is designed and optimized for I/O, and for
97 direct pixel access and manipulation. QImage supports several
98 image formats including monochrome, 8-bit, 32-bit and
99 alpha-blended images.
100
101 One advantage of using QImage as a paint device is that it is
102 possible to guarantee the pixel exactness of any drawing operation
103 in a platform-independent way. Another benefit is that the
104 painting can be performed in another thread than the current GUI
105 thread.
106
107 \row \li \b Pixmap
108
109 The QPixmap class is an off-screen image representation which is
110 designed and optimized for showing images on screen. Unlike
111 QImage, the pixel data in a pixmap is internal and is managed by
112 the underlying window system, i.e. pixels can only be accessed
113 through QPainter functions or by converting the QPixmap to a
114 QImage.
115
116 To optimize drawing with QPixmap, Qt provides the QPixmapCache
117 class which can be used to store temporary pixmaps that are
118 expensive to generate without using more storage space than the
119 cache limit.
120
121 Qt also provides the QBitmap convenience class, inheriting
122 QPixmap. QBitmap guarantees monochrome (1-bit depth) pixmaps, and
123 is mainly used for creating custom QCursor and QBrush objects,
124 constructing QRegion objects.
125
126 \row \li \b {OpenGL Paint Device}
127
128 As mentioned previously, Qt is offering classes that makes it easy
129 to use OpenGL in Qt applications. For example, the QOpenGLPaintDevice
130 enables the OpenGL API for rendering with QPainter.
131
132 \row \li \b {Picture}
133
134 The QPicture class is a paint device that records and replays
135 QPainter commands. A picture serializes painter commands to an IO
136 device in a platform-independent format. QPicture is also
137 resolution independent, i.e. a QPicture can be displayed on
138 different devices (for example svg, pdf, ps, printer and screen)
139 looking the same.
140
141 Qt provides the QPicture::load() and QPicture::save() functions
142 as well as streaming operators for loading and saving pictures.
143
144
145
146 \row \li \b {Custom Backends}
147
148 Support for a new backend can be implemented by deriving from the
149 QPaintDevice class and reimplementing the virtual
150 QPaintDevice::paintEngine() function to tell QPainter which paint
151 engine should be used to draw on this particular device. To
152 actually be able to draw on the device, this paint engine must be
153 a custom paint engine created by deriving from the QPaintEngine
154 class.
155
156 \endtable
157
158*/
159
160/*!
161 \page paintsystem-drawing.html
162 \title Drawing and Filling
163
164 \previouspage Paint Devices and Backends
165 \nextpage Coordinate System
166
167 \section1 Drawing
168
169 QPainter provides highly optimized functions to do most of the
170 drawing GUI programs require. It can draw everything from simple
171 graphical primitives (represented by the QPoint, QLine, QRect,
172 QRegion and QPolygon classes) to complex shapes like vector
173 paths. In Qt vector paths are represented by the QPainterPath
174 class. QPainterPath provides a container for painting operations,
175 enabling graphical shapes to be constructed and reused.
176
177 \table 100%
178 \row
179 \li \image paintsystem-painterpath.png {Qt logo}
180 \li \b QPainterPath
181
182 A painter path is an object composed of lines and curves. For
183 example, a rectangle is composed by lines and an ellipse is
184 composed by curves.
185
186 The main advantage of painter paths over normal drawing operations
187 is that complex shapes only need to be created once; then they can
188 be drawn many times using only calls to the QPainter::drawPath()
189 function.
190
191 A QPainterPath object can be used for filling, outlining, and
192 clipping. To generate fillable outlines for a given painter path,
193 use the QPainterPathStroker class.
194
195 \endtable
196
197 Lines and outlines are drawn using the QPen class. A pen is
198 defined by its style (i.e. its line-type), width, brush, how the
199 endpoints are drawn (cap-style) and how joins between two
200 connected lines are drawn (join-style). The pen's brush is a
201 QBrush object used to fill strokes generated with the pen,
202 i.e. the QBrush class defines the fill pattern.
203
204 QPainter can also draw aligned text and pixmaps.
205
206 When drawing text, the font is specified using the QFont class. Qt
207 will use the font with the specified attributes, or if no matching
208 font exists, Qt will use the closest matching installed font. The
209 attributes of the font that is actually used can be retrieved
210 using the QFontInfo class. In addition, the QFontMetrics class
211 provides the font measurements, and the QFontDatabase class
212 provides information about the fonts available in the underlying
213 window system.
214
215 Normally, QPainter draws in a "natural" coordinate system, but it
216 is able to perform view and world transformations using the
217 QTransform class. For more information, see \l {Coordinate
218 System}, which also describes the rendering process, i.e. the
219 relation between the logical representation and the rendered
220 pixels, and the benefits of anti-aliased painting.
221
222 \table 100%
223 \row \li
224 \b {Anti-Aliased Painting}
225
226 When drawing, the pixel rendering is controlled by the
227 QPainter::Antialiasing render hint. The QPainter::RenderHint enum
228 is used to specify flags to QPainter that may or may not be
229 respected by any given engine.
230
231 The QPainter::Antialiasing value indicates that the engine should
232 antialias edges of primitives if possible, i.e. smoothing the
233 edges by using different color intensities.
234
235 \li \image paintsystem-antialiasing.png {Screenshot showing a drawing
236 that was rendered with antialiasing}
237
238 \endtable
239
240 \section1 Filling
241
242 Shapes are filled using the QBrush class. A brush is defined
243 by its color and its style (i.e. its fill pattern).
244
245 Any color in Qt is represented by the QColor class which supports
246 the RGB, HSV and CMYK color models. QColor also support
247 alpha-blended outlining and filling (specifying the transparency
248 effect), and the class is platform and device independent. For more
249 information, see the QColor class documentation.
250
251 The available fill patterns are described by the Qt::BrushStyle
252 enum. These include basic patterns spanning from uniform color to
253 very sparse pattern, various line combinations, gradient fills and
254 textures. Qt provides the QGradient class to define custom
255 gradient fills, while texture patterns are specified using the
256 QPixmap class.
257
258 \table 100%
259 \row
260 \li \image paintsystem-fancygradient.png {Screenshot of the gradient
261 example}
262 \li \b QGradient
263
264 The QGradient class is used in combination with QBrush to specify
265 gradient fills.
266
267 \image paintsystem-gradients.png {Illustration that shows three
268 different supported ways gradients can be used}
269
270 Qt currently supports three types of gradient fills: Linear
271 gradients interpolate colors between start and end points, radial
272 gradients interpolate colors between a focal point and end points
273 on a circle surrounding it, and conical gradients interpolate
274 colors around a center point.
275
276 \endtable
277*/
278
279/*!
280 \page paintsystem-images.html
281 \title Reading and Writing Image Files
282
283 \previouspage Coordinate System
284
285 The most common way to read images is through QImage and QPixmap's
286 constructors, or by calling the QImage::load() and QPixmap::load()
287 functions. In addition, Qt provides the QImageReader class which
288 gives more control over the process. Depending on the underlying
289 support in the image format, the functions provided by the class
290 can save memory and speed up loading of images.
291
292 Likewise, Qt provides the QImageWriter class which supports
293 setting format specific options, such as the gamma level,
294 compression level and quality, prior to storing the image. If you
295 do not need such options, you can use QImage::save() or
296 QPixmap::save() instead.
297
298 \table 100%
299 \row
300 \li \b QMovie
301
302 QMovie is a convenience class for displaying animations, using the
303 QImageReader class internally. Once created, the QMovie class
304 provides various functions for both running and controlling the
305 given animation.
306
307 \li \image paintsystem-movie.png {Screenshot showing how creating
308 an animation with QMovie looks by separating the
309 animation into individual images}
310 \endtable
311
312 The QImageReader and QImageWriter classes rely on the
313 QImageIOHandler class which is the common image I/O interface for
314 all image formats in Qt. QImageIOHandler objects are used
315 internally by QImageReader and QImageWriter to add support for
316 different image formats to Qt.
317
318 A list of the supported file formats are available through the
319 QImageReader::supportedImageFormats() and
320 QImageWriter::supportedImageFormats() functions. Qt supports
321 several file formats by default, and in addition new formats can
322 be added as plugins. The currently supported formats are listed in
323 the QImageReader and QImageWriter class documentation.
324
325 Qt's plugin mechanism can also be used to write a custom image
326 format handler. This is done by deriving from the QImageIOHandler
327 class, and creating a QImageIOPlugin object which is a factory for
328 creating QImageIOHandler objects. When the plugin is installed,
329 QImageReader and QImageWriter will automatically load the plugin
330 and start using it.
331*/