Qt 6.x
The Qt SDK
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
14 void paintEvent(QPaintEvent *);
16};
17struct MyWidget : public QWidget
18{
19 void paintEvent(QPaintEvent *);
20};
22
23
26{
27 QPainter painter(this);
29 painter.setFont(QFont("Arial", 30));
31}
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};
68
71{
72 QPainter p(this);
73 p.drawLine(drawingCode); // drawing code
74}
76
77
78void wrapper0() {
79QPainter *painter = nullptr;
80QPainter *painter2 = nullptr;
81QPaintDevice *myWidget = nullptr;
83painter->begin(0); // impossible - paint device cannot be 0
84
85QPixmap image(0, 0);
86painter->begin(&image); // impossible - image.isNull() == true;
87
88painter->begin(myWidget);
89painter2->begin(myWidget); // impossible - only one painter at a time
91
92} // wrapper0
93
94namespace QPainterWrapper {
95struct QPainter {
98};
99
100} // QPainterWrapper
101
105path.moveTo(20, 80);
106path.lineTo(20, 30);
107path.cubicTo(80, 0, 50, 50, 80, 80);
108
109QPainter painter(this);
112
113
115QLineF line(10.0, 80.0, 90.0, 20.0);
116
117QPainter painter(this);
120} // MyWidget::wrapper1()
121
122
124
126QRectF rectangle(10.0, 20.0, 80.0, 60.0);
127
128QPainter painter(this);
129painter.drawRect(rectangle);
131
132} // MyWidget::wrapper2
133
134
136
138QRectF rectangle(10.0, 20.0, 80.0, 60.0);
139
140QPainter painter(this);
141painter.drawRoundedRect(rectangle, 20.0, 15.0);
143
144} // MyWidget::wrapper3
145
146
148
150QRectF rectangle(10.0, 20.0, 80.0, 60.0);
151
152QPainter painter(this);
153painter.drawEllipse(rectangle);
155
156} // MyWidget::wrapper4
157
158
160
162QRectF rectangle(10.0, 20.0, 80.0, 60.0);
163int startAngle = 30 * 16;
164int spanAngle = 120 * 16;
165
166QPainter painter(this);
167painter.drawArc(rectangle, startAngle, spanAngle);
169
170} // MyWidget::wrapper5
171
172
174
176QRectF rectangle(10.0, 20.0, 80.0, 60.0);
177int startAngle = 30 * 16;
178int spanAngle = 120 * 16;
179
180QPainter painter(this);
181painter.drawPie(rectangle, startAngle, spanAngle);
183
184} // MyWidget::wrapper6
185
186
188QRect rect;
189
191QRectF rectangle(10.0, 20.0, 80.0, 60.0);
192int startAngle = 30 * 16;
193int spanAngle = 120 * 16;
194
195QPainter painter(this);
196painter.drawChord(rect, startAngle, spanAngle);
198Q_UNUSED(rectangle);
199} // MyWidget::wrapper7
200
201
203
205static const QPointF points[3] = {
206 QPointF(10.0, 80.0),
207 QPointF(20.0, 10.0),
208 QPointF(80.0, 30.0),
209};
210
211QPainter painter(this);
214
215} // MyWidget::wrapper8
216
217
220static const QPointF points[4] = {
221 QPointF(10.0, 80.0),
222 QPointF(20.0, 10.0),
223 QPointF(80.0, 30.0),
224 QPointF(90.0, 70.0)
225};
226
227QPainter painter(this);
230
231} // MyWidget::wrapper9
232
233
235
237static const QPointF points[4] = {
238 QPointF(10.0, 80.0),
239 QPointF(20.0, 10.0),
240 QPointF(80.0, 30.0),
241 QPointF(90.0, 70.0)
242};
243
244QPainter painter(this);
247
248
250QRectF target(10.0, 20.0, 80.0, 60.0);
251QRectF source(0.0, 0.0, 70.0, 40.0);
252QPixmap pixmap(":myPixmap.png");
253
254QPainter painter(this);
257
258} // MyWidget::wrapper10
259
260
262QRect rect;
263
265QPainter painter(this);
266painter.drawText(rect, Qt::AlignCenter, tr("Qt\nProject"));
268
269} // MyWidget::wrapper11
270
271
272QRectF fillRect(QRect rect, int background) {
273 Q_UNUSED(rect);
274 Q_UNUSED(background);
275 return QRectF();
276};
278QRect rectangle;
279
282QPointF point(10.0, 20.0);
283picture.load("drawing.pic");
284
285QPainter painter(this);
288
289Q_UNUSED(point);
290
291
293fillRect(rectangle, background());
295
296} // MyWidget::wrapper12
297
298
300
302QRectF target(10.0, 20.0, 80.0, 60.0);
303QRectF source(0.0, 0.0, 70.0, 40.0);
304QImage image(":/images/myImage.png");
305
306QPainter painter(this);
309
310} // MyWidget::wrapper13
311
312
314
316QPainter painter(this);
317painter.fillRect(0, 0, 128, 128, Qt::green);
319
320glEnable(GL_SCISSOR_TEST);
321glScissor(0, 0, 64, 64);
322
323glClearColor(1, 0, 0, 1);
324glClear(GL_COLOR_BUFFER_BIT);
325
326glDisable(GL_SCISSOR_TEST);
327
330
331} // MyWidget::wrapper14
332
333
335
337QPainter painter(this);
341
342const QRect rectangle = QRect(0, 0, 100, 50);
344painter.drawText(rectangle, 0, tr("Hello"), &boundingRect);
345
346QPen pen = painter.pen();
348painter.setPen(pen);
349painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width()));
350
352painter.setPen(pen);
353painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width()));
355
356
357} // MyWidget::wrapper15
358} // src_gui_painting_qpainter2
\reentrant
Definition qfont.h:20
void setPixelSize(int)
Sets the font size to pixelSize pixels, with a maxiumum size of an unsigned 16-bit integer.
Definition qfont.cpp:1034
\inmodule QtGui
Definition qimage.h:37
\inmodule QtCore
Definition qline.h:182
\inmodule QtCore
Definition qline.h:18
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:485
\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.
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 drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
\inmodule QtGui
Definition qpen.h:25
void setStyle(Qt::PenStyle)
[0]
int width() const
Returns the pen width with integer precision.
Definition qpen.cpp:586
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:214
\inmodule QtCore\reentrant
Definition qrect.h:483
constexpr QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition qrect.h:799
\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:369
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:162
@ blue
Definition qnamespace.h:36
@ green
Definition qnamespace.h:35
@ 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:92
widget render & pixmap
QPainter painter(this)
[7]