Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_gui_painting_qpainter.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QOpenGLFunctions>
4#include <QPaintEvent>
5#include <QPainter>
6#include <QPainterPath>
7#include <QPicture>
8#include <QRectF>
9#include <QWidget>
10
11
17struct MyWidget : public QWidget
18{
19 void paintEvent(QPaintEvent *);
20};
22
23
33
34
37{
38 QPainter p;
39 p.begin(this);
40 p.drawLine(drawingCode); // drawing code
41 p.end();
42}
44
45} // src_gui_painting_qpainter
47struct MyWidget : public QWidget
48{
49 void paintEvent(QPaintEvent *);
50 int background() { return 0; }
51 void wrapper1();
52 void wrapper2();
53 void wrapper3();
54 void wrapper4();
55 void wrapper5();
56 void wrapper6();
57 void wrapper7();
58 void wrapper8();
59 void wrapper9();
60 void wrapper10();
61 void wrapper11();
62 void wrapper12();
63 void wrapper13();
64 void wrapper14();
65 void wrapper15();
66 void concentricCircles();
67};
69
72{
73 QPainter p(this);
74 p.drawLine(drawingCode); // drawing code
75}
77
78
79void wrapper0() {
80QPainter *painter = nullptr;
81QPainter *painter2 = nullptr;
82QPaintDevice *myWidget = nullptr;
84painter->begin(0); // impossible - paint device cannot be 0
85
86QPixmap image(0, 0);
87painter->begin(&image); // impossible - image.isNull() == true;
88
89painter->begin(myWidget);
90painter2->begin(myWidget); // impossible - only one painter at a time
92
93} // wrapper0
94
95namespace QPainterWrapper {
100
101} // QPainterWrapper
102
106path.moveTo(20, 80);
107path.lineTo(20, 30);
108path.cubicTo(80, 0, 50, 50, 80, 80);
109
110QPainter painter(this);
113
114
116QLineF line(10.0, 80.0, 90.0, 20.0);
117
118QPainter painter(this);
121} // MyWidget::wrapper1()
122
123
125
127QRectF rectangle(10.0, 20.0, 80.0, 60.0);
128
129QPainter painter(this);
130painter.drawRect(rectangle);
132
133} // MyWidget::wrapper2
134
135
137
139QRectF rectangle(10.0, 20.0, 80.0, 60.0);
140
141QPainter painter(this);
142painter.drawRoundedRect(rectangle, 20.0, 15.0);
144
145} // MyWidget::wrapper3
146
147
149
151QRectF rectangle(10.0, 20.0, 80.0, 60.0);
152
153QPainter painter(this);
154painter.drawEllipse(rectangle);
156
157} // MyWidget::wrapper4
158
159
161
163QRectF rectangle(10.0, 20.0, 80.0, 60.0);
164int startAngle = 30 * 16;
165int spanAngle = 120 * 16;
166
167QPainter painter(this);
168painter.drawArc(rectangle, startAngle, spanAngle);
170
171} // MyWidget::wrapper5
172
173
175
177QRectF rectangle(10.0, 20.0, 80.0, 60.0);
178int startAngle = 30 * 16;
179int spanAngle = 120 * 16;
180
181QPainter painter(this);
182painter.drawPie(rectangle, startAngle, spanAngle);
184
185} // MyWidget::wrapper6
186
187
189QRect rect;
190
192QRectF rectangle(10.0, 20.0, 80.0, 60.0);
193int startAngle = 30 * 16;
194int spanAngle = 120 * 16;
195
196QPainter painter(this);
197painter.drawChord(rect, startAngle, spanAngle);
199Q_UNUSED(rectangle);
200} // MyWidget::wrapper7
201
202
204
206static const QPointF points[3] = {
207 QPointF(10.0, 80.0),
208 QPointF(20.0, 10.0),
209 QPointF(80.0, 30.0),
210};
211
212QPainter painter(this);
215
216} // MyWidget::wrapper8
217
218
221static const QPointF points[4] = {
222 QPointF(10.0, 80.0),
223 QPointF(20.0, 10.0),
224 QPointF(80.0, 30.0),
225 QPointF(90.0, 70.0)
226};
227
228QPainter painter(this);
231
232} // MyWidget::wrapper9
233
234
236
238static const QPointF points[4] = {
239 QPointF(10.0, 80.0),
240 QPointF(20.0, 10.0),
241 QPointF(80.0, 30.0),
242 QPointF(90.0, 70.0)
243};
244
245QPainter painter(this);
248
249
251QRectF target(10.0, 20.0, 80.0, 60.0);
252QRectF source(0.0, 0.0, 70.0, 40.0);
253QPixmap pixmap(":myPixmap.png");
254
255QPainter painter(this);
258
259} // MyWidget::wrapper10
260
261
263QRect rect;
264
266QPainter painter(this);
267painter.drawText(rect, Qt::AlignCenter, tr("Qt\nProject"));
269
270} // MyWidget::wrapper11
271
272
273QRectF fillRect(QRect rect, int background) {
274 Q_UNUSED(rect);
275 Q_UNUSED(background);
276 return QRectF();
277};
279QRect rectangle;
280
283QPointF point(10.0, 20.0);
284picture.load("drawing.pic");
285
286QPainter painter(this);
289
290Q_UNUSED(point);
291
292
294fillRect(rectangle, background());
296
297} // MyWidget::wrapper12
298
299
301
303QRectF target(10.0, 20.0, 80.0, 60.0);
304QRectF source(0.0, 0.0, 70.0, 40.0);
305QImage image(":/images/myImage.png");
306
307QPainter painter(this);
310
311} // MyWidget::wrapper13
312
313
315
317QPainter painter(this);
318painter.fillRect(0, 0, 128, 128, Qt::green);
320
321glEnable(GL_SCISSOR_TEST);
322glScissor(0, 0, 64, 64);
323
324glClearColor(1, 0, 0, 1);
325glClear(GL_COLOR_BUFFER_BIT);
326
327glDisable(GL_SCISSOR_TEST);
328
331
332} // MyWidget::wrapper14
333
334
336
338QPainter painter(this);
342
343const QRect rectangle = QRect(0, 0, 100, 50);
345painter.drawText(rectangle, 0, tr("Hello"), &boundingRect);
346
347QPen pen = painter.pen();
349painter.setPen(pen);
350painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width()));
351
353painter.setPen(pen);
354painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width()));
356
357
358} // MyWidget::wrapper15
359
361{
363 QPainter painter(this);
366 int diameter = 50;
368 painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0, diameter, diameter));
371 painter.drawEllipse(QRect(-diameter / 2, -diameter / 2, diameter, diameter));
373
374} // MyWidget::concentricCircles
375
376} // src_gui_painting_qpainter2
\reentrant
Definition qfont.h:22
void setPixelSize(int)
Sets the font size to pixelSize pixels, with a maxiumum size of an unsigned 16-bit integer.
Definition qfont.cpp:1049
\inmodule QtGui
Definition qimage.h:37
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
\inmodule QtCore\compares equality \compareswith equality QLineF \endcompareswith
Definition qline.h:18
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
\inmodule QtGui
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
const QPen & pen() const
Returns the painter's current pen.
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
Definition qpainter.h:519
void drawPath(const QPainterPath &path)
Draws the given painter path using the current pen for outline and the current brush for filling.
void drawConvexPolygon(const QPointF *points, int pointCount)
Draws the convex polygon defined by the first pointCount points in the array points using the current...
void drawPie(const QRectF &rect, int a, int alen)
Draws a pie defined by the given rectangle, startAngle and spanAngle.
void setPen(const QColor &color)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void drawLine(const QLineF &line)
Draws a line defined by line.
Definition qpainter.h:442
void drawChord(const QRectF &rect, int a, int alen)
Draws the chord defined by the given rectangle, startAngle and spanAngle.
bool begin(QPaintDevice *)
Begins painting the paint device and returns true if successful; otherwise returns false.
const QFont & font() const
Returns the currently set font used for drawing text.
void beginNativePainting()
void drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect, Qt::ImageConversionFlags flags=Qt::AutoColor)
Draws the rectangular portion source of the given image into the target rectangle in the paint device...
void setFont(const QFont &f)
Sets the painter's font to the given font.
void drawText(const QPointF &p, const QString &s)
Draws the given text with the currently defined text direction, beginning at the given position.
void drawPolyline(const QPointF *points, int pointCount)
Draws the polyline defined by the first pointCount points in points using the current pen.
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device.
void drawArc(const QRectF &rect, int a, int alen)
Draws the arc defined by the given rectangle, startAngle and spanAngle.
void drawEllipse(const QRectF &r)
Draws the ellipse defined by the given rectangle.
@ Antialiasing
Definition qpainter.h:52
void drawPicture(const QPointF &p, const QPicture &picture)
Replays the given picture at the given point.
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule=Qt::OddEvenFill)
Draws the polygon defined by the first pointCount points in the array points using the current pen an...
void endNativePainting()
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint.
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
\inmodule QtGui
Definition qpen.h:28
void setStyle(Qt::PenStyle)
[0]
int width() const
Returns the pen width with integer precision.
Definition qpen.cpp:560
The QPicture class is a paint device that records and replays QPainter commands.
Definition qpicture.h:19
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr QRect adjusted(int x1, int y1, int x2, int y2) const noexcept
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition qrect.h:370
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QRect rect
the internal geometry of the widget excluding any window frame
Definition qwidget.h:116
QFont font
the font currently set for the widget
Definition qwidget.h:133
rect
[4]
@ AlignCenter
Definition qnamespace.h:163
@ blue
Definition qnamespace.h:37
@ green
Definition qnamespace.h:36
@ DotLine
@ DashLine
Definition image.cpp:4
QRectF fillRect(QRect rect, int background)
GLfloat angle
GLenum target
GLsizei GLsizei GLchar * source
GLfixed GLfixed GLint GLint GLfixed points
GLuint GLenum matrix
GLsizei const GLchar *const * path
GLfloat GLfloat p
[1]
static const QRectF boundingRect(const QPointF *points, int pointCount)
#define tr(X)
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
widget render & pixmap
QPainter painter(this)
[7]