source: trunk/doc/src/porting/qt4-arthur.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 12.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at qt-info@nokia.com.
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \page qt4-arthur.html
30 \title The Arthur Paint System
31
32 \contentspage {What's New in Qt 4}{Home}
33 \previouspage The Interview Framework
34 \nextpage The Scribe Classes
35
36 This document describes Qt 4's painting system, providing a
37 comparison between the approaches used by Qt when rendering
38 graphics in Qt 3 and Qt 4.
39
40 \tableofcontents
41
42 \section1 Architecture
43
44 The Qt 4 Paint System is primarily based on the classes
45 QPainter, QPaintDevice, and QPaintEngine. QPainter is the
46 class used to perform drawing operations, such as drawLine()
47 and drawRect(). QPaintDevice represents a device that can be
48 painted on using a QPainter; both QWidget and QPixmap are
49 QPaintDevices. QPaintEngine provides the interface that the
50 painter uses to draw onto different types of devices.
51
52 \section2 A Look Back at Qt 3
53
54 In Qt 3, QPainter could be used to draw on widgets and pixmaps.
55 (It could also be used to draw to printers on Windows and Mac OS
56 X.) When other paint devices needed to be supported, such as
57 QPrinter on X11, this was done by deriving from QPaintDevice and
58 reimplementing the virtual function QPaintDevice::cmd(). A
59 reimplemented paint device was treated as an external device.
60
61 QPainter was capable of recognizing external devices and could
62 serialize each paint operation to the reimplemented cmd()
63 function. This allowed reimplementation of arbitrary devices, but
64 the approach has some disadvantages which we have addressed in
65 Qt 4. One of these is that an external device could not reuse any
66 functionality implemented in QPainter since QPainter was tied to
67 widget/pixmap painting on that platform. Supporting multiple
68 device backends, such as OpenGL, was therefore inconvenient and
69 not very efficient.
70
71 This has led us to devise a more convenient and intuitive API for
72 Qt 4.
73
74 \section2 How Painting is Done in Qt 4
75
76 In Qt 4 we have introduced the QPaintEngine abstract class.
77 Implementations of this class provide the concrete functionality
78 needed to draw to specific device types. The QPaintEngine class
79 is only used internally by QPainter and QPaintDevice, and it is
80 hidden from application programmers unless they reimplement their own
81 device types for their own QPaintEngine subclasses. Qt currently
82 provides paint engines for the following platforms and APIs:
83
84 \list
85 \o A pixel-based engine for the Windows platform that is
86 also used to draw onto QImages on all platforms
87 \o OpenGL on all platforms
88 \o PostScript on Linux, Unix, and Mac OS X
89 \o QuickDraw and CoreGraphics on Mac OS X
90 \o X11 and the X Render Extension on Linux and Unix systems
91 \omit
92 \o QVFb, VNC, and LinuxFb for Qt for Embedded Linux
93 \endomit
94 \endlist
95
96 To implement support for a new backend, you must derive from
97 QPaintEngine and reimplement its virtual functions. You also need
98 to derive from QPaintDevice and reimplement the virtual function
99 QPaintDevice::paintEngine() to tell QPainter which paint engine
100 should be used to draw on this particular device.
101
102 The main benefit of this approach is that all painting follows the
103 same painting pipeline. This means that adding support for new features
104 and providing default implementations for unsupported ones has
105 become much simpler.
106
107 \section1 New Features in the Qt 4 Paint System
108
109 \section2 Gradient Brushes
110
111 With Qt 4 it is possible to fill shapes using gradient
112 brushes. A gradient in this case is used to describe the transition
113 from one color at a given point to different color at another point. A
114 gradient can span from one color to another or over a
115 number of colors by specifying multiple colors at positions in the
116 gradient area. Qt 4 supports linear, radial, and conical gradients.
117
118 Linear gradients are specified using two control points.
119 Setting a linear gradient brush is done by creating a QLinearGradient
120 object and setting it as a brush.
121
122 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 0
123
124 The code shown above produces a pattern as show in the following
125 pixmap:
126
127 \img diagonalGradient.png
128
129 Radial gradients are specified using a center, a radius, and a
130 focal point. Setting a radial brush is done by creating a QRadialGradient
131 object and setting it as a brush.
132
133 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 1
134
135 The code shown above produces a pattern as shown in the following
136 pixmap:
137
138 \img radialGradient.png
139
140 Conical gradients are specified using a center and a start
141 angle. Setting a conical brush is done by creating a
142 QConicalGradient object and setting it as a brush.
143
144 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 2
145
146 The code shown above produces a pattern as shown in the following
147 pixmap:
148
149 \img conicalGradient.png
150
151 \section2 Alpha-Blended Drawing
152
153 With Qt 4 we support alpha-blended outlining and filling. The
154 alpha channel of a color is defined through QColor. The alpha
155 channel specifies the transparency effect, 0 represents a fully
156 transparent color, while 255 represents a fully opaque color. For
157 example:
158
159 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 3
160
161 The code shown above produces the following output:
162
163 \img alphafill.png
164
165 Alpha-blended drawing is supported on Windows, Mac OS X, and on
166 X11 systems that have the X Render extension installed.
167
168
169 \section2 QPainter and QGLWidget
170
171 It is now possible to open a QPainter on a QGLWidget as if it
172 were a normal QWidget. One huge benefit from this is that we
173 utilize the high performance of OpenGL for most drawing
174 operations, such as transformations and pixmap drawing.
175
176
177 \section2 Anti-Aliased Edges
178
179 On platforms where this is supported by the native drawing API, we
180 provide the option of turning on anti-aliased edges when drawing
181 graphics primitives.
182
183 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 4
184
185 This produces the following output:
186
187 \img antialiased.png
188
189 Anti-aliasing is supported when drawing to a QImage and on all
190 systems, except on X11 when XRender is not present.
191
192
193 \section2 Extensive Use of Native Graphics Operations
194
195 Where this makes sense, Qt uses native graphics
196 operations. The benefit we gain from this is that these operations
197 can potentially be performed in hardware, giving significant
198 speed improvements over many pure-software implementations.
199
200 Among these are native transformations (Mac OS X and OpenGL),
201 making painting with a world matrix much faster. Some pixmap
202 operations have also been moved closer to the underlying
203 hardware implementations.
204
205
206 \section2 Painter Paths
207
208 A painter path is an object composed of a number of graphical
209 building blocks, such as rectangles, ellipses, lines, and curves.
210 A painter path can be used for filling, outlining, and for clipping.
211 The main advantage of painter paths over normal drawing operations
212 is that it is possible to build up non-linear shapes which can be
213 drawn later in one go.
214
215 Building blocks can be joined in closed subpaths, such as a
216 rectangle or an ellipse, or they can exist independently as unclosed
217 subpaths, although an unclosed path will not be filled.
218
219 Below is a code example on how a path can be used. The
220 painter in this case has a pen width of 3 and a light blue brush. We
221 first add a rectangle, which becomes a closed subpath. We then add
222 two bezier curves, and finally draw the entire path.
223
224 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 5
225
226 The code above produces the following output:
227
228 \img pathexample.png
229
230
231 \section2 Widget Double-Buffering
232
233 In Qt 4, all widgets are double-buffered by default.
234
235 In previous versions of Qt double-buffering was achieved by
236 painting to an off-screen pixmap then copying the pixmap to the
237 screen. For example:
238
239 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 6
240
241 Since the double-buffering is handled by QWidget internally this
242 now becomes:
243
244 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 7
245
246 Double-buffering is turned on by default, but can be turned off for
247 individual widgets by setting the widget attribute
248 Qt::WA_PaintOnScreen.
249
250 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 8
251
252 \section2 Pen and Brush Transformation
253
254 In Qt 3, pens and brushes weren't affected by the painter's
255 transformation matrix. For example, if you drew a rectangle with a
256 pen width of 1 using a scaled painter, the resulting line width
257 would still be 1. This made it difficult to implement features
258 such as zooming and high-resolution printing.
259
260 In Qt 4, pens and brushes honor the painter's transformation
261 matrix.
262
263 Note that this feature is still in development and not yet
264 supported on all platforms.
265
266 \section2 Custom Filled Pens
267
268 In Qt 4, it is possible to specify how an outline should be
269 filled. It can be a solid color or a QBrush, which makes it
270 possible to specify both texture and gradient fills for both
271 text and outlines.
272
273 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 9
274
275 The code above produces the following output:
276
277 \img gradientText.png
278
279 \section2 QImage as a Paint Device
280
281 A great improvement of Qt 4 over previous versions it that it now
282 provides a pixel-based raster paint engine which allows users to
283 open a painter on a QImage. The QImage paint engine supports the
284 full feature set of QPainter (paths, antialiasing, alphablending,
285 etc.) and can be used on all platforms.
286
287 One advantage of this is that it is possible to guarantee the
288 pixel exactness of any drawing operation in a platform-independent
289 way.
290
291 Painting on an image is as simple as drawing on any other paint device.
292
293 \snippet doc/src/snippets/code/doc_src_qt4-arthur.qdoc 10
294
295 \section2 SVG Rendering Support
296
297 \l{Scalable Vector Graphics} (SVG) is an language for describing both static
298 and animated two-dimensional vector graphics. Qt includes support for the
299 \l{SVG 1.2 Tiny Static Features}{static features} of \l{SVG 1.2 Tiny}, taking
300 advantage of the improved paint system in Qt 4. SVG drawings can be rendered
301 onto any QPaintDevice subclass, such as QWidget, QImage, and QGLWidget, to
302 take advantage of specific advantages of each device. This approach gives
303 developers the flexibility to experiment, in order to find the best solution
304 for each application.
305
306 \image svg-image.png
307
308 Since SVG is an XML-based format, the QtXml module is required to read SVG
309 files. For this reason, classes for SVG handling are provided separately in
310 the QtSvg module.
311
312 Displaying an SVG drawing in an application is as simple as displaying a
313 bitmap image. QSvgWidget is a display widget that can be placed in an
314 appropriate place in a user interface, and new content can be loaded as
315 required. For example, a predetermined file can be loaded and displayed in
316 a widget with little effort:
317
318 \snippet doc/src/snippets/qsvgwidget/main.cpp 0
319
320 For applications with more specialized requirements, the QSvgRenderer class
321 provides more control over the way SVG drawings are rendered and animated.
322*/
Note: See TracBrowser for help on using the repository browser.