summaryrefslogtreecommitdiffstats
path: root/doc/src/examples/2dpainting.qdoc
blob: 738b4d94e5b9a318b7e238e889166592936744b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example opengl/2dpainting
    \title 2D Painting Example

    The 2D Painting example shows how QPainter and QGLWidget can be used
    together to display accelerated 2D graphics on supported hardware.

    \image 2dpainting-example.png

    The QPainter class is used to draw 2D graphics primitives onto
    paint devices provided by QPaintDevice subclasses, such as QWidget
    and QImage.

    Since QGLWidget is a subclass of QWidget, it is possible
    to reimplement its \l{QWidget::paintEvent()}{paintEvent()} and use
    QPainter to draw on the device, just as you would with a QWidget.
    The only difference is that the painting operations will be accelerated
    in hardware if it is supported by your system's OpenGL drivers.

    In this example, we perform the same painting operations on a
    QWidget and a QGLWidget. The QWidget is shown with anti-aliasing
    enabled, and the QGLWidget will also use anti-aliasing if the
    required extensions are supported by your system's OpenGL driver.

    \section1 Overview

    To be able to compare the results of painting onto a QGLWidget subclass
    with native drawing in a QWidget subclass, we want to show both kinds
    of widget side by side. To do this, we derive subclasses of QWidget and
    QGLWidget, using a separate \c Helper class to perform the same painting
    operations for each, and lay them out in a top-level widget, itself
    provided a the \c Window class.

    \section1 Helper Class Definition

    In this example, the painting operations are performed by a helper class.
    We do this because we want the same painting operations to be performed
    for both our QWidget subclass and the QGLWidget subclass.

    The \c Helper class is minimal:

    \snippet examples/opengl/2dpainting/helper.h 0

    Apart from the constructor, it only provides a \c paint() function to paint
    using a painter supplied by one of our widget subclasses.

    \section1 Helper Class Implementation

    The constructor of the class sets up the resources it needs to paint
    content onto a widget:

    \snippet examples/opengl/2dpainting/helper.cpp 0

    The actual painting is performed in the \c paint() function. This takes
    a QPainter that has already been set up to paint onto a paint device
    (either a QWidget or a QGLWidget), a QPaintEvent that provides information
    about the region to be painted, and a measure of the elapsed time (in
    milliseconds) since the paint device was last updated.

    \snippet examples/opengl/2dpainting/helper.cpp 1

    We begin painting by filling in the region contained in the paint event
    before translating the origin of the coordinate system so that the rest
    of the painting operations will be displaced towards the center of the
    paint device.

    We draw a spiral pattern of circles, using the elapsed time specified to
    animate them so that they appear to move outward and around the coordinate
    system's origin:

    \snippet examples/opengl/2dpainting/helper.cpp 2

    Since the coordinate system is rotated many times during
    this process, we \l{QPainter::save()}{save()} the QPainter's state
    beforehand and \l{QPainter::restore()}{restore()} it afterwards.

    \snippet examples/opengl/2dpainting/helper.cpp 3

    We draw some text at the origin to complete the effect.

    \section1 Widget Class Definition

    The \c Widget class provides a basic custom widget that we use to
    display the simple animation painted by the \c Helper class.

    \snippet examples/opengl/2dpainting/widget.h 0

    Apart from the constructor, it only contains a
    \l{QWidget::paintEvent()}{paintEvent()} function, that lets us draw
    customized content, and a slot that is used to animate its contents.
    One member variable keeps track of the \c Helper that the widget uses
    to paint its contents, and the other records the elapsed time since
    it was last updated.

    \section1 Widget Class Implementation

    The constructor only initializes the member variables, storing the
    \c Helper object supplied and calling the base class's constructor,
    and enforces a fixed size for the widget:

    \snippet examples/opengl/2dpainting/widget.cpp 0

    The \c animate() slot is called whenever a timer, which we define later, times
    out:

    \snippet examples/opengl/2dpainting/widget.cpp 1

    Here, we determine the interval that has elapsed since the timer last
    timed out, and we add it to any existing value before repainting the
    widget. Since the animation used in the \c Helper class loops every second,
    we can use the modulo operator to ensure that the \c elapsed variable is
    always less than 1000.

    Since the \c Helper class does all of the actual painting, we only have
    to implement a paint event that sets up a QPainter for the widget and calls
    the helper's \c paint() function:

    \snippet examples/opengl/2dpainting/widget.cpp 2

    \section1 GLWidget Class Definition

    The \c GLWidget class definition is basically the same as the \c Widget
    class except that it is derived from QGLWidget.

    \snippet examples/opengl/2dpainting/glwidget.h 0

    Again, the member variables record the \c Helper used to paint the
    widget and the elapsed time since the previous update.

    \section1 GLWidget Class Implementation

    The constructor differs a little from the \c Widget class's constructor:

    \snippet examples/opengl/2dpainting/glwidget.cpp 0

    As well as initializing the \c elapsed member variable and storing the
    \c Helper object used to paint the widget, the base class's constructor
    is called with the format that specifies the \l QGL::SampleBuffers flag.
    This enables anti-aliasing if it is supported by your system's OpenGL
    driver.

    The \c animate() slot is exactly the same as that provided by the \c Widget
    class:

    \snippet examples/opengl/2dpainting/glwidget.cpp 1

    The \c paintEvent() is almost the same as that found in the \c Widget class:

    \snippet examples/opengl/2dpainting/glwidget.cpp 2

    Since anti-aliasing will be enabled if available, we only need to set up
    a QPainter on the widget and call the helper's \c paint() function to display
    the widget's contents.

    \section1 Window Class Definition

    The \c Window class has a basic, minimal definition:

    \snippet examples/opengl/2dpainting/window.h 0

    It contains a single \c Helper object that will be shared between all
    widgets.

    \section1 Window Class Implementation

    The constructor does all the work, creating a widget of each type and
    inserting them with labels into a layout:

    \snippet examples/opengl/2dpainting/window.cpp 0

    A timer with a 50 millisecond time out is constructed for animation purposes,
    and connected to the \c animate() slots of the \c Widget and \c GLWidget objects.
    Once started, the widgets should be updated at around 20 frames per second.

    \section1 Running the Example

    The example shows the same painting operations performed at the same time
    in a \c Widget and a \c GLWidget. The quality and speed of rendering in the
    \c GLWidget depends on the level of support for multisampling and hardware
    acceleration that your system's OpenGL driver provides. If support for either
    of these is lacking, the driver may fall back on a software renderer that
    may trade quality for speed.
*/