Skip to content

Commit

Permalink
Use QPainterStateGuard in examples
Browse files Browse the repository at this point in the history
Complements 9ecf47a.

Pick-to: 6.9
Change-Id: I65456f8fd34bf9d316b72c4286e1b15789309f7c
Reviewed-by: Christian Ehrlicher <[email protected]>
  • Loading branch information
FriedemannKleint committed Dec 17, 2024
1 parent 5731fe0 commit 1dc15c1
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 55 deletions.
9 changes: 4 additions & 5 deletions examples/widgets/doc/src/analogclock.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@
system counterclockwise by a number of degrees determined by the current
hour and minute. This means that the hand will be shown rotated clockwise
by the required amount.
We save and restore the transformation matrix before and after the
rotation because we want to place the minute hand without having to
take into account any previous rotations.
We save and restore the transformation matrix before and after the rotation
by instantiating a QPainterStateGuard because we want to place the minute
hand without having to take into account any previous rotations.

\snippet widgets/analogclock/analogclock.cpp 17
\snippet widgets/analogclock/analogclock.cpp 19

We draw markers around the edge of the clock for each hour in the same
Expand All @@ -123,7 +122,7 @@

\snippet widgets/analogclock/analogclock.cpp 21

For the seconds hand we do the same and add two cicles as a visual highlight.
For the seconds hand we do the same and add two circles as a visual highlight.

\snippet widgets/analogclock/analogclock.cpp 23
\codeline
Expand Down
10 changes: 5 additions & 5 deletions examples/widgets/doc/src/basicdrawing.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@
RenderArea widget, and we calculate their positions using two \c
for loops and the widgets height and width.

For each copy we first save the current painter state (pushes the
state onto a stack). Then we translate the coordinate system,
For each copy we first save the current painter state by instantiating
a QPainterStateGuard. Then we translate the coordinate system,
using the QPainter::translate() function, to the position
determined by the variables of the \c for loops. If we omit this
translation of the coordinate system all the copies of the shape
Expand Down Expand Up @@ -420,9 +420,9 @@

\snippet painting/basicdrawing/renderarea.cpp 13

Then, when we are finished rendering a copy of the shape we can
restore the original painter state, with its associated coordinate
system, using the QPainter::restore() function. In this way we
Then, when we are finished rendering a copy of the shape,
the original painter state is restored by QPainterStateGuard,
which calls the QPainter::restore() function. In this way, we
ensure that the next shape copy will be rendered in the correct
position.

Expand Down
12 changes: 5 additions & 7 deletions examples/widgets/doc/src/transformations.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,11 @@

\snippet painting/transformations/renderarea.cpp 6

Before we start to render the shape, we call the QPainter::save()
function.

QPainter::save() saves the current painter state (i.e. pushes the
state onto a stack) including the current coordinate system. The
rationale for saving the painter state is that the following call
to the \c transformPainter() function will transform the
Before we start to render the shape, we instantiate
a QPainterStateGuard to save the current painter state (i.e. push the
state onto a stack) including the current coordinate system while
in scope. The rationale for saving the painter state is that the
following call to the \c transformPainter() function will transform the
coordinate system depending on the currently chosen transformation
operations, and we need a way to get back to the original state to
draw the outline.
Expand Down
4 changes: 2 additions & 2 deletions examples/widgets/painting/affine/xform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <QLayout>
#include <QPainter>
#include <QPainterStateGuard>
#include <QPainterPath>

const int alpha = 155;
Expand Down Expand Up @@ -76,7 +77,7 @@ void XFormView::resizeEvent(QResizeEvent *e)

void XFormView::paint(QPainter *p)
{
p->save();
QPainterStateGuard guard(p);
p->setRenderHint(QPainter::Antialiasing);
p->setRenderHint(QPainter::SmoothPixmapTransform);
switch (m_type) {
Expand All @@ -90,7 +91,6 @@ void XFormView::paint(QPainter *p)
drawTextType(p);
break;
}
p->restore();
}

void XFormView::updateControlPoints(const QPolygonF &points)
Expand Down
4 changes: 2 additions & 2 deletions examples/widgets/painting/basicdrawing/renderarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QPainter>
#include <QPainterPath>
#include <QPainterStateGuard>

//! [0]
RenderArea::RenderArea(QWidget *parent)
Expand Down Expand Up @@ -106,7 +107,7 @@ void RenderArea::paintEvent(QPaintEvent * /* event */)
//! [10]
for (int x = 0; x < width(); x += 100) {
for (int y = 0; y < height(); y += 100) {
painter.save();
QPainterStateGuard guard(&painter);
painter.translate(x, y);
//! [10] //! [11]
if (transformed) {
Expand Down Expand Up @@ -161,7 +162,6 @@ void RenderArea::paintEvent(QPaintEvent * /* event */)
painter.drawPixmap(10, 10, pixmap);
}
//! [12] //! [13]
painter.restore();
}
}

Expand Down
10 changes: 6 additions & 4 deletions examples/widgets/painting/transformations/renderarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "renderarea.h"

#include <QPainter>
#include <QPainterStateGuard>
#include <QPaintEvent>

//! [0]
Expand Down Expand Up @@ -61,10 +62,11 @@ void RenderArea::paintEvent(QPaintEvent *event)
//! [5]

//! [6]
painter.save();
transformPainter(painter);
drawShape(painter);
painter.restore();
{
QPainterStateGuard guard(&painter);
transformPainter(painter);
drawShape(painter);
}
//! [6]

//! [7]
Expand Down
35 changes: 19 additions & 16 deletions examples/widgets/widgets/analogclock/analogclock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "analogclock.h"

#include <QPainter>
#include <QPainterStateGuard>
#include <QTime>
#include <QTimer>

Expand Down Expand Up @@ -78,12 +79,12 @@ void AnalogClock::paintEvent(QPaintEvent *)
//! [16]
//! [18]

//! [17]
painter.save();
//! [17] //! [19]
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 4);
painter.restore();
//! [19]
{
QPainterStateGuard guard(&painter);
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 4);
}
//! [18] //! [19]

//! [20]
Expand All @@ -97,10 +98,11 @@ void AnalogClock::paintEvent(QPaintEvent *)
painter.setBrush(minuteColor);

//! [22]
painter.save();
painter.rotate(6.0 * time.minute());
painter.drawConvexPolygon(minuteHand, 4);
painter.restore();
{
QPainterStateGuard guard(&painter);
painter.rotate(6.0 * time.minute());
painter.drawConvexPolygon(minuteHand, 4);
}
//! [21] //! [22]


Expand All @@ -109,12 +111,13 @@ void AnalogClock::paintEvent(QPaintEvent *)
//! [23]

//! [24]
painter.save();
painter.rotate(6.0 * time.second());
painter.drawConvexPolygon(secondsHand, 4);
painter.drawEllipse(-3, -3, 6, 6);
painter.drawEllipse(-5, -68, 10, 10);
painter.restore();
{
QPainterStateGuard guard(&painter);
painter.rotate(6.0 * time.second());
painter.drawConvexPolygon(secondsHand, 4);
painter.drawEllipse(-3, -3, 6, 6);
painter.drawEllipse(-5, -68, 10, 10);
}
//! [24]

//! [25]
Expand Down
32 changes: 18 additions & 14 deletions examples/widgets/widgets/shapedclock/shapedclock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QCoreApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QPainterStateGuard>
#include <QTime>
#include <QTimer>

Expand Down Expand Up @@ -93,10 +94,11 @@ void ShapedClock::paintEvent(QPaintEvent *)
painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);

painter.save();
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 4);
painter.restore();
{
QPainterStateGuard guard(&painter);
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 4);
}

for (int i = 0; i < 12; ++i) {
painter.drawRect(73, -3, 16, 6);
Expand All @@ -105,19 +107,21 @@ void ShapedClock::paintEvent(QPaintEvent *)

painter.setBrush(minuteColor);

painter.save();
painter.rotate(6.0 * time.minute());
painter.drawConvexPolygon(minuteHand, 4);
painter.restore();
{
QPainterStateGuard guard(&painter);
painter.rotate(6.0 * time.minute());
painter.drawConvexPolygon(minuteHand, 4);
}

painter.setBrush(secondsColor);

painter.save();
painter.rotate(6.0 * time.second());
painter.drawConvexPolygon(secondsHand, 4);
painter.drawEllipse(-3, -3, 6, 6);
painter.drawEllipse(-5, -68, 10, 10);
painter.restore();
{
QPainterStateGuard guard(&painter);
painter.rotate(6.0 * time.second());
painter.drawConvexPolygon(secondsHand, 4);
painter.drawEllipse(-3, -3, 6, 6);
painter.drawEllipse(-5, -68, 10, 10);
}

painter.setPen(minuteColor);

Expand Down

0 comments on commit 1dc15c1

Please sign in to comment.