Update copyright year in license headers.
[profile/ivi/qtbase.git] / doc / src / snippets / code / src_gui_painting_qpainter.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 //! [0]
42 void SimpleExampleWidget::paintEvent(QPaintEvent *)
43 {
44     QPainter painter(this);
45     painter.setPen(Qt::blue);
46     painter.setFont(QFont("Arial", 30));
47     painter.drawText(rect(), Qt::AlignCenter, "Qt");
48 }
49 //! [0]
50
51
52 //! [1]
53 void MyWidget::paintEvent(QPaintEvent *)
54 {
55     QPainter p;
56     p.begin(this);
57     p.drawLine(...);        // drawing code
58     p.end();
59 }
60 //! [1]
61
62
63 //! [2]
64 void MyWidget::paintEvent(QPaintEvent *)
65 {
66     QPainter p(this);
67     p.drawLine(...);        // drawing code
68 }
69 //! [2]
70
71
72 //! [3]
73 painter->begin(0); // impossible - paint device cannot be 0
74
75 QPixmap image(0, 0);
76 painter->begin(&image); // impossible - image.isNull() == true;
77
78 painter->begin(myWidget);
79 painter2->begin(myWidget); // impossible - only one painter at a time
80 //! [3]
81
82
83 //! [4]
84 void QPainter::rotate(qreal angle)
85 {
86     QMatrix matrix;
87     matrix.rotate(angle);
88     setWorldMatrix(matrix, true);
89 }
90 //! [4]
91
92
93 //! [5]
94 QPainterPath path;
95 path.moveTo(20, 80);
96 path.lineTo(20, 30);
97 path.cubicTo(80, 0, 50, 50, 80, 80);
98
99 QPainter painter(this);
100 painter.drawPath(path);
101 //! [5]
102
103
104 //! [6]
105 QLineF line(10.0, 80.0, 90.0, 20.0);
106
107 QPainter(this);
108 painter.drawLine(line);
109 //! [6]
110
111
112 //! [7]
113 QRectF rectangle(10.0, 20.0, 80.0, 60.0);
114
115 QPainter painter(this);
116 painter.drawRect(rectangle);
117 //! [7]
118
119
120 //! [8]
121 QRectF rectangle(10.0, 20.0, 80.0, 60.0);
122
123 QPainter painter(this);
124 painter.drawRoundedRect(rectangle, 20.0, 15.0);
125 //! [8]
126
127
128 //! [9]
129 QRectF rectangle(10.0, 20.0, 80.0, 60.0);
130
131 QPainter painter(this);
132 painter.drawEllipse(rectangle);
133 //! [9]
134
135
136 //! [10]
137 QRectF rectangle(10.0, 20.0, 80.0, 60.0);
138 int startAngle = 30 * 16;
139 int spanAngle = 120 * 16;
140
141 QPainter painter(this);
142 painter.drawArc(rectangle, startAngle, spanAngle);
143 //! [10]
144
145
146 //! [11]
147 QRectF rectangle(10.0, 20.0, 80.0, 60.0);
148 int startAngle = 30 * 16;
149 int spanAngle = 120 * 16;
150
151 QPainter painter(this);
152 painter.drawPie(rectangle, startAngle, spanAngle);
153 //! [11]
154
155
156 //! [12]
157 QRectF rectangle(10.0, 20.0, 80.0, 60.0);
158 int startAngle = 30 * 16;
159 int spanAngle = 120 * 16;
160
161 QPainter painter(this);
162 painter.drawChord(rect, startAngle, spanAngle);
163 //! [12]
164
165
166 //! [13]
167 static const QPointF points[3] = {
168     QPointF(10.0, 80.0),
169     QPointF(20.0, 10.0),
170     QPointF(80.0, 30.0),
171 };
172
173 QPainter painter(this);
174 painter.drawPolyline(points, 3);
175 //! [13]
176
177
178 //! [14]
179 static const QPointF points[4] = {
180     QPointF(10.0, 80.0),
181     QPointF(20.0, 10.0),
182     QPointF(80.0, 30.0),
183     QPointF(90.0, 70.0)
184 };
185
186 QPainter painter(this);
187 painter.drawPolygon(points, 4);
188 //! [14]
189
190
191 //! [15]
192 static const QPointF points[4] = {
193     QPointF(10.0, 80.0),
194     QPointF(20.0, 10.0),
195     QPointF(80.0, 30.0),
196     QPointF(90.0, 70.0)
197 };
198
199 QPainter painter(this);
200 painter.drawConvexPolygon(points, 4);
201 //! [15]
202
203
204 //! [16]
205 QRectF target(10.0, 20.0, 80.0, 60.0);
206 QRectF source(0.0, 0.0, 70.0, 40.0);
207 QPixmap pixmap(":myPixmap.png");
208
209 QPainter(this);
210 painter.drawPixmap(target, image, source);
211 //! [16]
212
213
214 //! [17]
215 QPainter painter(this);
216 painter.drawText(rect, Qt::AlignCenter, tr("Qt by\nNokia"));
217 //! [17]
218
219
220 //! [18]
221 QPicture picture;
222 QPointF point(10.0, 20.0)
223 picture.load("drawing.pic");
224
225 QPainter painter(this);
226 painter.drawPicture(0, 0, picture);
227 //! [18]
228
229
230 //! [19]
231 fillRect(rectangle, background()).
232 //! [19]
233
234
235 //! [20]
236 QRectF target(10.0, 20.0, 80.0, 60.0);
237 QRectF source(0.0, 0.0, 70.0, 40.0);
238 QImage image(":/images/myImage.png");
239
240 QPainter painter(this);
241 painter.drawImage(target, image, source);
242 //! [20]
243
244
245 //! [21]
246 QPainter painter(this);
247 painter.fillRect(0, 0, 128, 128, Qt::green);
248 painter.beginNativePainting();
249
250 glEnable(GL_SCISSOR_TEST);
251 glScissor(0, 0, 64, 64);
252
253 glClearColor(1, 0, 0, 1);
254 glClear(GL_COLOR_BUFFER_BIT);
255
256 glDisable(GL_SCISSOR_TEST);
257
258 painter.endNativePainting();
259 //! [21]