Use QPainterStateGuard in examples

Complements 9ecf47a8a8d11227ecf192246d7df7c2c4dc9105.

Pick-to: 6.9
Change-Id: I65456f8fd34bf9d316b72c4286e1b15789309f7c
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Friedemann Kleint 2024-12-17 11:12:13 +01:00
parent 5731fe051e
commit 1dc15c11db
8 changed files with 61 additions and 55 deletions

View File

@ -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
@ -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

View File

@ -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
@ -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.

View File

@ -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.

View File

@ -6,6 +6,7 @@
#include <QLayout>
#include <QPainter>
#include <QPainterStateGuard>
#include <QPainterPath>
const int alpha = 155;
@ -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) {
@ -90,7 +91,6 @@ void XFormView::paint(QPainter *p)
drawTextType(p);
break;
}
p->restore();
}
void XFormView::updateControlPoints(const QPolygonF &points)

View File

@ -5,6 +5,7 @@
#include <QPainter>
#include <QPainterPath>
#include <QPainterStateGuard>
//! [0]
RenderArea::RenderArea(QWidget *parent)
@ -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) {
@ -161,7 +162,6 @@ void RenderArea::paintEvent(QPaintEvent * /* event */)
painter.drawPixmap(10, 10, pixmap);
}
//! [12] //! [13]
painter.restore();
}
}

View File

@ -4,6 +4,7 @@
#include "renderarea.h"
#include <QPainter>
#include <QPainterStateGuard>
#include <QPaintEvent>
//! [0]
@ -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]

View File

@ -4,6 +4,7 @@
#include "analogclock.h"
#include <QPainter>
#include <QPainterStateGuard>
#include <QTime>
#include <QTimer>
@ -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]
@ -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]
@ -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]

View File

@ -7,6 +7,7 @@
#include <QCoreApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QPainterStateGuard>
#include <QTime>
#include <QTimer>
@ -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);
@ -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);