1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the documentation of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 \page paintsystem.html
45 \title The Paint System
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.
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.
59 \image paintsystem-core.png
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
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.
72 \tableofcontents section1
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.
86 \o \image paintsystem-painterpath.png
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
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()
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.
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.
111 QPainter can also draw aligned text and pixmaps.
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
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.
131 \bold {Anti-Aliased Painting}
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.
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.
142 \o \image paintsystem-antialiasing.png
148 Shapes are filled using the QBrush class. A brush is defined
149 by its color and its style (i.e. its fill pattern).
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.
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.
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
173 \o \image paintsystem-fancygradient.png
176 The QGradient class is used in combination with QBrush to specify
179 \image paintsystem-gradients.png
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.
189 \section1 Creating a Paint Device
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.
197 \image paintsystem-devices.png
200 \row \o \bold {Custom Backends}
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
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.
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.
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
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
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
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
253 \section2 OpenGL Widget
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
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
266 \section2 Pixel Buffer
268 The QtOpenGL module also provides the QGLPixelBuffer class which
269 inherits QPaintDevice directly.
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.
275 \section2 Framebuffer Object
277 The QtOpenGL module also provides the QGLFramebufferObject class
278 which inherits QPaintDevice directly.
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.
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)
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.
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.
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.
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
317 \section1 Reading and Writing Image Files
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.
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.
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
341 \o \image paintsystem-movie.png
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.
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.
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
366 \o \image paintsystem-svg.png
367 \o \bold {SVG Rendering}
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.
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.
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
383 For more information, see the QtSvg module documentation.
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.
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
401 Most functions for drawing style elements take four arguments:
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)
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
418 \image paintsystem-stylepainter.png
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.
429 \o \inlineimage paintsystem-icon.png
432 The QIcon class provides scalable icons in different modes and states.
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.
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
446 \section1 Selecting the Painting Backend
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:
455 \o Software Rasterizer
464 \o Software Rasterizer
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.
473 > analogclock -graphicssystem raster
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.
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.