Skip to content

Commit

Permalink
Doc: Compile painting snippets
Browse files Browse the repository at this point in the history
Fix minor issues (e.g. whitespace, missing semi-colon) in passing.

Change-Id: I223cb257539d2ac4cb0a1ea209f0c3cebddb3d54
Done-with: Nico Vertriest <[email protected]>
Task-number: QTBUG-81486
Reviewed-by: Topi Reiniö <[email protected]>
  • Loading branch information
paulwicking committed Aug 28, 2020
1 parent 176e7d4 commit 4853cff
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/gui/doc/snippets/code/code.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ SOURCES = \
src_gui_math3d_qquaternion.cpp \
src_gui_opengl_qopenglbuffer.cpp \
src_gui_opengl_qopengldebug.cpp \
src_gui_opengl_qopenglfunctions.cpp
src_gui_opengl_qopenglfunctions.cpp \
src_gui_painting_qbrush.cpp \
src_gui_painting_qcolor.cpp \
src_gui_painting_qpainter.cpp \
src_gui_painting_qpainterpath.cpp \
src_gui_painting_qpen.cpp \
src_gui_painting_qregion.cpp \
src_gui_painting_qregion_unix.cpp \
src_gui_painting_qtransform.cpp
13 changes: 13 additions & 0 deletions src/gui/doc/snippets/code/src_gui_painting_qbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QPainter>

namespace src_gui_painting_qbrush {

struct Wrapper : public QPaintDevice {
void wrapper();
};
void Wrapper::wrapper() {


//! [0]
QPainter painter(this);
Expand All @@ -59,3 +68,7 @@ painter.setBrush(Qt::NoBrush);
painter.setPen(Qt::darkGreen);
painter.drawRect(40, 40, 100, 100);
//! [0]


} // Wrapper::wrapper
} // src_gui_painting_qbrush
16 changes: 14 additions & 2 deletions src/gui/doc/snippets/code/src_gui_painting_qcolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,25 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QPainter>

namespace src_gui_painting_qcolor {
int width() { return 1; };
int height() { return 1; };
QPainter painter;
void wrapper() {


//! [0]
// Specify semi-transparent red
painter.setBrush(QColor(255, 0, 0, 127));
painter.drawRect(0, 0, width()/2, height());
painter.drawRect(0, 0, width() / 2, height());

// Specify semi-transparent blue
painter.setBrush(QColor(0, 0, 255, 127));
painter.drawRect(0, 0, width(), height()/2);
painter.drawRect(0, 0, width(), height() / 2);
//! [0]


} // wrapper
} // src_gui_painting_qcolor
133 changes: 129 additions & 4 deletions src/gui/doc/snippets/code/src_gui_painting_qpainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QOpenGLFunctions>
#include <QPaintEvent>
#include <QPainter>
#include <QPainterPath>
#include <QPicture>
#include <QRectF>
#include <QWidget>


namespace src_gui_painting_qpainter {
struct SimpleExampleWidget : public QPaintDevice {
void paintEvent(QPaintEvent *);
QRect rect();
};
struct MyWidget : public QWidget
{
void paintEvent(QPaintEvent *);
};
QLine drawingCode;


//! [0]
void SimpleExampleWidget::paintEvent(QPaintEvent *)
Expand All @@ -64,21 +84,48 @@ void MyWidget::paintEvent(QPaintEvent *)
{
QPainter p;
p.begin(this);
p.drawLine(...); // drawing code
p.drawLine(drawingCode); // drawing code
p.end();
}
//! [1]

} // src_gui_painting_qpainter
namespace src_gui_painting_qpainter2 {
struct MyWidget : public QWidget
{
void paintEvent(QPaintEvent *);
int background() { return 0; }
void wrapper1();
void wrapper2();
void wrapper3();
void wrapper4();
void wrapper5();
void wrapper6();
void wrapper7();
void wrapper8();
void wrapper9();
void wrapper10();
void wrapper11();
void wrapper12();
void wrapper13();
void wrapper14();
void wrapper15();
};
QLine drawingCode;

//! [2]
void MyWidget::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawLine(...); // drawing code
p.drawLine(drawingCode); // drawing code
}
//! [2]


void wrapper0() {
QPainter *painter = nullptr;
QPainter *painter2 = nullptr;
QPaintDevice *myWidget = nullptr;
//! [3]
painter->begin(0); // impossible - paint device cannot be 0

Expand All @@ -89,6 +136,13 @@ painter->begin(myWidget);
painter2->begin(myWidget); // impossible - only one painter at a time
//! [3]

} // wrapper0

namespace QPainterWrapper {
struct QPainter {
void rotate(qreal angle);
void setWorldTransform(QTransform matrix, bool);
};

//! [4]
void QPainter::rotate(qreal angle)
Expand All @@ -99,7 +153,9 @@ void QPainter::rotate(qreal angle)
}
//! [4]

} // QPainterWrapper

void MyWidget::wrapper1() {
//! [5]
QPainterPath path;
path.moveTo(20, 80);
Expand All @@ -117,15 +173,22 @@ QLineF line(10.0, 80.0, 90.0, 20.0);
QPainter(this);
painter.drawLine(line);
//! [6]
} // MyWidget::wrapper1()


void MyWidget::wrapper2() {

//! [7]
QRectF rectangle(10.0, 20.0, 80.0, 60.0);

QPainter painter(this);
painter.drawRect(rectangle);
//! [7]

} // MyWidget::wrapper2


void MyWidget::wrapper3() {

//! [8]
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
Expand All @@ -134,6 +197,10 @@ QPainter painter(this);
painter.drawRoundedRect(rectangle, 20.0, 15.0);
//! [8]

} // MyWidget::wrapper3


void MyWidget::wrapper4() {

//! [9]
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
Expand All @@ -142,6 +209,10 @@ QPainter painter(this);
painter.drawEllipse(rectangle);
//! [9]

} // MyWidget::wrapper4


void MyWidget::wrapper5() {

//! [10]
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
Expand All @@ -152,6 +223,10 @@ QPainter painter(this);
painter.drawArc(rectangle, startAngle, spanAngle);
//! [10]

} // MyWidget::wrapper5


void MyWidget::wrapper6() {

//! [11]
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
Expand All @@ -162,6 +237,11 @@ QPainter painter(this);
painter.drawPie(rectangle, startAngle, spanAngle);
//! [11]

} // MyWidget::wrapper6


void MyWidget::wrapper7() {
QRect rect;

//! [12]
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
Expand All @@ -171,8 +251,12 @@ int spanAngle = 120 * 16;
QPainter painter(this);
painter.drawChord(rect, startAngle, spanAngle);
//! [12]
Q_UNUSED(rectangle);
} // MyWidget::wrapper7


void MyWidget::wrapper8() {

//! [13]
static const QPointF points[3] = {
QPointF(10.0, 80.0),
Expand All @@ -184,7 +268,10 @@ QPainter painter(this);
painter.drawPolyline(points, 3);
//! [13]

} // MyWidget::wrapper8


void MyWidget::wrapper9() {
//! [14]
static const QPointF points[4] = {
QPointF(10.0, 80.0),
Expand All @@ -197,6 +284,10 @@ QPainter painter(this);
painter.drawPolygon(points, 4);
//! [14]

} // MyWidget::wrapper9


void MyWidget::wrapper10() {

//! [15]
static const QPointF points[4] = {
Expand All @@ -220,27 +311,48 @@ QPainter(this);
painter.drawPixmap(target, pixmap, source);
//! [16]

} // MyWidget::wrapper10


void MyWidget::wrapper11() {
QRect rect;

//! [17]
QPainter painter(this);
painter.drawText(rect, Qt::AlignCenter, tr("Qt\nProject"));
//! [17]

} // MyWidget::wrapper11


QRectF fillRect(QRect rect, int background) {
Q_UNUSED(rect);
Q_UNUSED(background);
return QRectF();
};
void MyWidget::wrapper12() {
QRect rectangle;

//! [18]
QPicture picture;
QPointF point(10.0, 20.0)
QPointF point(10.0, 20.0);
picture.load("drawing.pic");

QPainter painter(this);
painter.drawPicture(0, 0, picture);
//! [18]

Q_UNUSED(point);


//! [19]
fillRect(rectangle, background()).
fillRect(rectangle, background());
//! [19]

} // MyWidget::wrapper12


void MyWidget::wrapper13() {

//! [20]
QRectF target(10.0, 20.0, 80.0, 60.0);
Expand All @@ -251,6 +363,10 @@ QPainter painter(this);
painter.drawImage(target, image, source);
//! [20]

} // MyWidget::wrapper13


void MyWidget::wrapper14() {

//! [21]
QPainter painter(this);
Expand All @@ -268,6 +384,11 @@ glDisable(GL_SCISSOR_TEST);
painter.endNativePainting();
//! [21]

} // MyWidget::wrapper14


void MyWidget::wrapper15() {

//! [drawText]
QPainter painter(this);
QFont font = painter.font();
Expand All @@ -287,3 +408,7 @@ pen.setStyle(Qt::DashLine);
painter.setPen(pen);
painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width()));
//! [drawText]


} // MyWidget::wrapper15
} // src_gui_painting_qpainter2
Loading

0 comments on commit 4853cff

Please sign in to comment.