source: trunk/doc/src/paintsystem.qdoc@ 321

Last change on this file since 321 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 19.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page paintsystem.html
44
45 \title The Paint System
46
47 Qt's paint system enables painting on screen and print devices
48 using the same API, and is primarily based on the QPainter,
49 QPaintDevice, and QPaintEngine classes.
50
51 QPainter is used to perform drawing operations, QPaintDevice is an
52 abstraction of a two-dimensional space that can be painted on
53 using a QPainter, and QPaintEngine provides the interface that the
54 painter uses to draw onto different types of devices. The
55 QPaintEngine class is used internally by QPainter and
56 QPaintDevice, and is hidden from application programmers unless
57 they create their own device type.
58
59 \image paintsystem-core.png
60
61 The main benefit of this approach is that all painting follows the
62 same painting pipeline making it easy to add support for new
63 features and providing default implementations for unsupported
64 ones.
65
66 Alternatively, Qt provides the QtOpenGL module, offering classes
67 that makes it easy to use OpenGL in Qt applications. Among others,
68 the module provides an OpenGL widget class that can be used just
69 like any other Qt widget, except that it opens an OpenGL display
70 buffer where the OpenGL API can be used to render the contents.
71
72 \tableofcontents section1
73
74 \section1 Drawing
75
76 QPainter provides highly optimized functions to do most of the
77 drawing GUI programs require. It can draw everything from simple
78 graphical primitives (represented by the QPoint, QLine, QRect,
79 QRegion and QPolygon classes) to complex shapes like vector
80 paths. In Qt vector paths are represented by the QPainterPath
81 class. QPainterPath provides a container for painting operations,
82 enabling graphical shapes to be constructed and reused.
83
84 \table 100%
85 \row
86 \o \image paintsystem-painterpath.png
87 \o \bold QPainterPath
88
89 A painter path is an object composed of lines and curves. For
90 example, a rectangle is composed by lines and an ellipse is
91 composed by curves.
92
93 The main advantage of painter paths over normal drawing operations
94 is that complex shapes only need to be created once; then they can
95 be drawn many times using only calls to the QPainter::drawPath()
96 function.
97
98 A QPainterPath object can be used for filling, outlining, and
99 clipping. To generate fillable outlines for a given painter path,
100 use the QPainterPathStroker class.
101
102 \endtable
103
104 Lines and outlines are drawn using the QPen class. A pen is
105 defined by its style (i.e. its line-type), width, brush, how the
106 endpoints are drawn (cap-style) and how joins between two
107 connected lines are drawn (join-style). The pen's brush is a
108 QBrush object used to fill strokes generated with the pen,
109 i.e. the QBrush class defines the fill pattern.
110
111 QPainter can also draw aligned text and pixmaps.
112
113 When drawing text, the font is specified using the QFont class. Qt
114 will use the font with the specified attributes, or if no matching
115 font exists, Qt will use the closest matching installed font. The
116 attributes of the font that is actually used can be retrieved
117 using the QFontInfo class. In addition, the QFontMetrics class
118 provides the font measurements, and the QFontDatabase class
119 provides information about the fonts available in the underlying
120 window system.
121
122 Normally, QPainter draws in a "natural" coordinate system, but it
123 is able to perform view and world transformations using the
124 QMatrix class. For more information, see \l {The Coordinate
125 System} documentation which also describes the rendering process,
126 i.e. the relation between the logical representation and the
127 rendered pixels, and the benefits of anti-aliased painting.
128
129 \table 100%
130 \row \o
131 \bold {Anti-Aliased Painting}
132
133 When drawing, the pixel rendering is controlled by the
134 QPainter::Antialiasing render hint. The QPainter::RenderHint enum
135 is used to specify flags to QPainter that may or may not be
136 respected by any given engine.
137
138 The QPainter::Antialiasing value indicates that the engine should
139 antialias edges of primitives if possible, i.e. smoothing the
140 edges by using different color intensities.
141
142 \o \image paintsystem-antialiasing.png
143
144 \endtable
145
146 \section1 Filling
147
148 Shapes are filled using the QBrush class. A brush is defined
149 by its color and its style (i.e. its fill pattern).
150
151 Any color in Qt is represented by the QColor class which supports
152 the RGB, HSV and CMYK color models. QColor also support
153 alpha-blended outlining and filling (specifying the transparency
154 effect), and the class is platform and device independent (the
155 colors are mapped to hardware using the QColormap class). For more
156 information, see the QColor class documentation.
157
158 When creating a new widget, it is recommend to use the colors in
159 the widget's palette rather than hard-coding specific colors. All
160 widgets in Qt contain a palette and use their palette to draw
161 themselves. A widget's palette is represented by the QPalette
162 class which contains color groups for each widget state.
163
164 The available fill patterns are described by the Qt::BrushStyle
165 enum. These include basic patterns spanning from uniform color to
166 very sparse pattern, various line combinations, gradient fills and
167 textures. Qt provides the QGradient class to define custom
168 gradient fills, while texture patterns are specified using the
169 QPixmap class.
170
171 \table 100%
172 \row
173 \o \image paintsystem-fancygradient.png
174 \o \bold QGradient
175
176 The QGradient class is used in combination with QBrush to specify
177 gradient fills.
178
179 \image paintsystem-gradients.png
180
181 Qt currently supports three types of gradient fills: Linear
182 gradients interpolate colors between start and end points, radial
183 gradients interpolate colors between a focal point and end points
184 on a circle surrounding it, and conical gradients interpolate
185 colors around a center point.
186
187 \endtable
188
189 \section1 Creating a Paint Device
190
191 The QPaintDevice class is the base class of objects that can be
192 painted, i.e. QPainter can draw on any QPaintDevice
193 subclass. QPaintDevice's drawing capabilities are currently
194 implemented by the QWidget, QImage, QPixmap, QGLWidget,
195 QGLPixelBuffer, QPicture and QPrinter subclasses.
196
197 \image paintsystem-devices.png
198
199 \table 100%
200 \row \o \bold {Custom Backends}
201
202 Support for a new backend can be implemented by deriving from the
203 QPaintDevice class and reimplementing the virtual
204 QPaintDevice::paintEngine() function to tell QPainter which paint
205 engine should be used to draw on this particular device. To
206 actually be able to draw on the device, this paint engine must be
207 a custom paint engine created by deriving from the QPaintEngine
208 class.
209
210 \endtable
211
212 \section2 Widget
213
214 The QWidget class is the base class of all user interface
215 objects. The widget is the atom of the user interface: it receives
216 mouse, keyboard and other events from the window system, and
217 paints a representation of itself on the screen.
218
219 \section2 Image
220
221 The QImage class provides a hardware-independent image
222 representation which is designed and optimized for I/O, and for
223 direct pixel access and manipulation. QImage supports several
224 image formats including monochrome, 8-bit, 32-bit and
225 alpha-blended images.
226
227 One advantage of using QImage as a paint device is that it is
228 possible to guarantee the pixel exactness of any drawing operation
229 in a platform-independent way. Another benefit is that the
230 painting can be performed in another thread than the current GUI
231 thread.
232
233 \section2 Pixmap
234
235 The QPixmap class is an off-screen image representation which is
236 designed and optimized for showing images on screen. Unlike
237 QImage, the pixel data in a pixmap is internal and is managed by
238 the underlying window system, i.e. pixels can only be accessed
239 through QPainter functions or by converting the QPixmap to a
240 QImage.
241
242 To optimize drawing with QPixmap, Qt provides the QPixmapCache
243 class which can be used to store temporary pixmaps that are
244 expensive to generate without using more storage space than the
245 cache limit.
246
247 Qt also provides the QBitmap convenience class, inheriting
248 QPixmap. QBitmap guarantees monochrome (1-bit depth) pixmaps, and
249 is mainly used for creating custom QCursor and QBrush objects,
250 constructing QRegion objects, and for setting masks for pixmaps
251 and widgets.
252
253 \section2 OpenGL Widget
254
255 As mentioned above, Qt provides the QtOpenGL module offering
256 classes that makes it easy to use OpenGL in Qt applications. For
257 example, the QGLWidget enables the OpenGL API for
258 rendering.
259
260 But QGLWidget is also a QWidget subclass, and can be used by
261 QPainter as any other paint device. One huge benefit from this is
262 that it enables Qt to utilize the high performance of OpenGL for
263 most drawing operations, such as transformations and pixmap
264 drawing.
265
266 \section2 Pixel Buffer
267
268 The QtOpenGL module also provides the QGLPixelBuffer class which
269 inherits QPaintDevice directly.
270
271 QGLPixelBuffer encapsulates an OpenGL pbuffer. Rendering into a
272 pbuffer is normally done using full hardware acceleration which
273 can be significantly faster than rendering into a QPixmap.
274
275 \section2 Framebuffer Object
276
277 The QtOpenGL module also provides the QGLFramebufferObject class
278 which inherits QPaintDevice directly.
279
280 QGLFramebufferObject encapsulates an OpenGL framebuffer object.
281 Framebuffer objects can also be used for off-screen rendering, and
282 offer several advantages over pixel buffers for this purpose.
283 These are described in the QGLFramebufferObject class documentation.
284
285 \section2 Picture
286
287 The QPicture class is a paint device that records and replays
288 QPainter commands. A picture serializes painter commands to an IO
289 device in a platform-independent format. QPicture is also
290 resolution independent, i.e. a QPicture can be displayed on
291 different devices (for example svg, pdf, ps, printer and screen)
292 looking the same.
293
294 Qt provides the QPicture::load() and QPicture::save() functions
295 for loading and saving pictures. But in addition the QPictureIO
296 class is provided to enable the programmer to install new picture
297 file formats in addition to those that Qt provides.
298
299 \section2 Printer
300
301 The QPrinter class is a paint device that paints on a printer. On
302 Windows or Mac OS X, QPrinter uses the built-in printer
303 drivers. On X11, QPrinter generates postscript and sends that to
304 lpr, lp, or another print program. QPrinter can also print to any
305 other QPrintEngine object.
306
307 The QPrintEngine class defines an interface for how QPrinter
308 interacts with a given printing subsystem. The common case when
309 creating your own print engine, is to derive from both
310 QPaintEngine and QPrintEngine.
311
312 The output format is by default determined by the platform the
313 printer is running on, but by explicitly setting the output format
314 to QPrinter::PdfFormat, QPrinter will generate its output as a PDF
315 file.
316
317 \section1 Reading and Writing Image Files
318
319 The most common way to read images is through QImage and QPixmap's
320 constructors, or by calling the QImage::load() and QPixmap::load()
321 functions. In addition, Qt provides the QImageReader class which
322 gives more control over the process. Depending on the underlying
323 support in the image format, the functions provided by the class
324 can save memory and speed up loading of images.
325
326 Likewise, Qt provides the QImageWriter class which supports
327 setting format specific options, such as the gamma level,
328 compression level and quality, prior to storing the image. If you
329 do not need such options, you can use QImage::save() or
330 QPixmap::save() instead.
331
332 \table 100%
333 \row
334 \o \bold QMovie
335
336 QMovie is a convenience class for displaying animations, using the
337 QImageReader class internally. Once created, the QMovie class
338 provides various functions for both running and controlling the
339 given animation.
340
341 \o \image paintsystem-movie.png
342 \endtable
343
344 The QImageReader and QImageWriter classes rely on the
345 QImageIOHandler class which is the common image I/O interface for
346 all image formats in Qt. QImageIOHandler objects are used
347 internally by QImageReader and QImageWriter to add support for
348 different image formats to Qt.
349
350 A list of the supported file formats are available through the
351 QImageReader::supportedImageFormats() and
352 QImageWriter::supportedImageFormats() functions. Qt supports
353 several file formats by default, and in addition new formats can
354 be added as plugins. The currently supported formats are listed in
355 the QImageReader and QImageWriter class documentation.
356
357 Qt's plugin mechanism can also be used to write a custom image
358 format handler. This is done by deriving from the QImageIOHandler
359 class, and creating a QImageIOPlugin object which is a factory for
360 creating QImageIOHandler objects. When the plugin is installed,
361 QImageReader and QImageWriter will automatically load the plugin
362 and start using it.
363
364 \table 100%
365 \row
366 \o \image paintsystem-svg.png
367 \o \bold {SVG Rendering}
368
369 Scalable Vector Graphics (SVG) is an language for describing both
370 static and animated two-dimensional vector graphics. Qt includes
371 support for the static features of SVG 1.2 Tiny.
372
373 SVG drawings can be rendered onto any QPaintDevice subclass. This
374 approach gives developers the flexibility to experiment, in order
375 to find the best solution for each application.
376
377 The easiest way to render SVG files is to construct a QSvgWidget
378 and load an SVG file using one of the QSvgWidget::load()
379 functions. The rendering is performed by the QSvgRenderer class
380 which also can be used directly to provide SVG support for custom
381 widgets.
382
383 For more information, see the QtSvg module documentation.
384
385 \endtable
386
387 \section1 Styling
388
389 Qt's built-in widgets use the QStyle class to perform nearly all
390 of their drawing. QStyle is an abstract base class that
391 encapsulates the look and feel of a GUI, and can be used to make
392 the widgets look exactly like the equivalent native widgets or to
393 give the widgets a custom look.
394
395 Qt provides a set of QStyle subclasses that emulate the native
396 look of the different platforms supported by Qt (QWindowsStyle,
397 QMacStyle, QMotifStyle, etc.). These styles are built into the
398 QtGui library, other styles can be made available using Qt's
399 plugin mechansim.
400
401 Most functions for drawing style elements take four arguments:
402
403 \list
404 \o an enum value specifying which graphical element to draw
405 \o a QStyleOption object specifying how and where to render that element
406 \o a QPainter object that should be used to draw the element
407 \o a QWidget object on which the drawing is performed (optional)
408 \endlist
409
410 The style gets all the information it needs to render the
411 graphical element from the QStyleOption class. The widget is
412 passed as the last argument in case the style needs it to perform
413 special effects (such as animated default buttons on Mac OS X),
414 but it isn't mandatory. In fact, QStyle can be used to draw on any
415 paint device (not just widgets), in which case the widget argument
416 is a zero pointer.
417
418 \image paintsystem-stylepainter.png
419
420 The paint system also provides the QStylePainter class inheriting
421 from QPainter. QStylePainter is a convenience class for drawing
422 QStyle elements inside a widget, and extends QPainter with a set
423 of high-level drawing functions implemented on top of QStyle's
424 API. The advantage of using QStylePainter is that the parameter
425 lists get considerably shorter.
426
427 \table 100%
428 \row
429 \o \inlineimage paintsystem-icon.png
430 \o \bold QIcon
431
432 The QIcon class provides scalable icons in different modes and states.
433
434 QIcon can generate pixmaps reflecting an icon's state, mode and
435 size. These pixmaps are generated from the set of pixmaps
436 made available to the icon, and are used by Qt widgets to show an
437 icon representing a particular action.
438
439 The rendering of a QIcon object is handled by the QIconEngine
440 class. Each icon has a corresponding icon engine that is
441 responsible for drawing the icon with a requested size, mode and
442 state.
443
444 \endtable
445
446 \section1 Selecting the Painting Backend
447
448 Since Qt 4.5, it is possible to replace the paint engines and paint
449 devices used for widgets, pixmaps and the offscreen double buffer. By
450 default the backends are:
451
452 \table
453 \row
454 \o Windows
455 \o Software Rasterizer
456 \row
457 \o X11
458 \o X11
459 \row
460 \o Mac OS X
461 \o CoreGraphics
462 \row
463 \o Embedded
464 \o Software Rasterizer
465 \endtable
466
467 Passing a command line parameter to the application, such as,
468 \c{-graphicssystem raster}, specifies that Qt should use the software
469 rasterizer for this application. The Software rasterizer is fully
470 supported on all platforms.
471
472 \code
473 > analogclock -graphicssystem raster
474 \endcode
475
476 There is also a \c{-graphicssystem opengl} mode that uses OpenGL for
477 all drawing. Currently, this engine is experimental as it does not draw
478 everything correctly.
479
480 Qt also supports being configured using \c {-graphicssystem
481 raster|opengl} in which case all applications will use the
482 specified graphics system for its graphics.
483
484 */
485
Note: See TracBrowser for help on using the repository browser.