diff options
author | Friedemann Kleint <[email protected]> | 2024-12-17 11:12:13 +0100 |
---|---|---|
committer | Friedemann Kleint <[email protected]> | 2024-12-17 19:59:01 +0100 |
commit | 1dc15c11db88f96a916258acea80a86bfa7c87cd (patch) | |
tree | b9f260b60f259414612c463688f554f330c91f83 | |
parent | 5731fe051e48e7a256ef31ae93cfb89ce8d871cc (diff) |
Use QPainterStateGuard in examples
Complements 9ecf47a8a8d11227ecf192246d7df7c2c4dc9105.
Pick-to: 6.9
Change-Id: I65456f8fd34bf9d316b72c4286e1b15789309f7c
Reviewed-by: Christian Ehrlicher <[email protected]>
-rw-r--r-- | examples/widgets/doc/src/analogclock.qdoc | 9 | ||||
-rw-r--r-- | examples/widgets/doc/src/basicdrawing.qdoc | 10 | ||||
-rw-r--r-- | examples/widgets/doc/src/transformations.qdoc | 12 | ||||
-rw-r--r-- | examples/widgets/painting/affine/xform.cpp | 4 | ||||
-rw-r--r-- | examples/widgets/painting/basicdrawing/renderarea.cpp | 4 | ||||
-rw-r--r-- | examples/widgets/painting/transformations/renderarea.cpp | 10 | ||||
-rw-r--r-- | examples/widgets/widgets/analogclock/analogclock.cpp | 35 | ||||
-rw-r--r-- | examples/widgets/widgets/shapedclock/shapedclock.cpp | 32 |
8 files changed, 61 insertions, 55 deletions
diff --git a/examples/widgets/doc/src/analogclock.qdoc b/examples/widgets/doc/src/analogclock.qdoc index 8f98f4b7b08..aeaddd4b936 100644 --- a/examples/widgets/doc/src/analogclock.qdoc +++ b/examples/widgets/doc/src/analogclock.qdoc @@ -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 diff --git a/examples/widgets/doc/src/basicdrawing.qdoc b/examples/widgets/doc/src/basicdrawing.qdoc index 0882c0f32f3..a4606a29814 100644 --- a/examples/widgets/doc/src/basicdrawing.qdoc +++ b/examples/widgets/doc/src/basicdrawing.qdoc @@ -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. diff --git a/examples/widgets/doc/src/transformations.qdoc b/examples/widgets/doc/src/transformations.qdoc index d90708340c1..f8a29407c83 100644 --- a/examples/widgets/doc/src/transformations.qdoc +++ b/examples/widgets/doc/src/transformations.qdoc @@ -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. diff --git a/examples/widgets/painting/affine/xform.cpp b/examples/widgets/painting/affine/xform.cpp index 5754490eed9..d8aea6516ab 100644 --- a/examples/widgets/painting/affine/xform.cpp +++ b/examples/widgets/painting/affine/xform.cpp @@ -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) diff --git a/examples/widgets/painting/basicdrawing/renderarea.cpp b/examples/widgets/painting/basicdrawing/renderarea.cpp index d32abb52062..89fafa560e9 100644 --- a/examples/widgets/painting/basicdrawing/renderarea.cpp +++ b/examples/widgets/painting/basicdrawing/renderarea.cpp @@ -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(); } } diff --git a/examples/widgets/painting/transformations/renderarea.cpp b/examples/widgets/painting/transformations/renderarea.cpp index f967db6ee64..1de84d4a582 100644 --- a/examples/widgets/painting/transformations/renderarea.cpp +++ b/examples/widgets/painting/transformations/renderarea.cpp @@ -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] diff --git a/examples/widgets/widgets/analogclock/analogclock.cpp b/examples/widgets/widgets/analogclock/analogclock.cpp index feb7f4e8b58..548fbcb460a 100644 --- a/examples/widgets/widgets/analogclock/analogclock.cpp +++ b/examples/widgets/widgets/analogclock/analogclock.cpp @@ -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] diff --git a/examples/widgets/widgets/shapedclock/shapedclock.cpp b/examples/widgets/widgets/shapedclock/shapedclock.cpp index 106b3454a67..a5f32af24c4 100644 --- a/examples/widgets/widgets/shapedclock/shapedclock.cpp +++ b/examples/widgets/widgets/shapedclock/shapedclock.cpp @@ -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); |