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:
parent
5731fe051e
commit
1dc15c11db
@ -106,11 +106,10 @@
|
|||||||
system counterclockwise by a number of degrees determined by the current
|
system counterclockwise by a number of degrees determined by the current
|
||||||
hour and minute. This means that the hand will be shown rotated clockwise
|
hour and minute. This means that the hand will be shown rotated clockwise
|
||||||
by the required amount.
|
by the required amount.
|
||||||
We save and restore the transformation matrix before and after the
|
We save and restore the transformation matrix before and after the rotation
|
||||||
rotation because we want to place the minute hand without having to
|
by instantiating a QPainterStateGuard because we want to place the minute
|
||||||
take into account any previous rotations.
|
hand without having to take into account any previous rotations.
|
||||||
|
|
||||||
\snippet widgets/analogclock/analogclock.cpp 17
|
|
||||||
\snippet widgets/analogclock/analogclock.cpp 19
|
\snippet widgets/analogclock/analogclock.cpp 19
|
||||||
|
|
||||||
We draw markers around the edge of the clock for each hour in the same
|
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
|
\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
|
\snippet widgets/analogclock/analogclock.cpp 23
|
||||||
\codeline
|
\codeline
|
||||||
|
@ -370,8 +370,8 @@
|
|||||||
RenderArea widget, and we calculate their positions using two \c
|
RenderArea widget, and we calculate their positions using two \c
|
||||||
for loops and the widgets height and width.
|
for loops and the widgets height and width.
|
||||||
|
|
||||||
For each copy we first save the current painter state (pushes the
|
For each copy we first save the current painter state by instantiating
|
||||||
state onto a stack). Then we translate the coordinate system,
|
a QPainterStateGuard. Then we translate the coordinate system,
|
||||||
using the QPainter::translate() function, to the position
|
using the QPainter::translate() function, to the position
|
||||||
determined by the variables of the \c for loops. If we omit this
|
determined by the variables of the \c for loops. If we omit this
|
||||||
translation of the coordinate system all the copies of the shape
|
translation of the coordinate system all the copies of the shape
|
||||||
@ -420,9 +420,9 @@
|
|||||||
|
|
||||||
\snippet painting/basicdrawing/renderarea.cpp 13
|
\snippet painting/basicdrawing/renderarea.cpp 13
|
||||||
|
|
||||||
Then, when we are finished rendering a copy of the shape we can
|
Then, when we are finished rendering a copy of the shape,
|
||||||
restore the original painter state, with its associated coordinate
|
the original painter state is restored by QPainterStateGuard,
|
||||||
system, using the QPainter::restore() function. In this way we
|
which calls the QPainter::restore() function. In this way, we
|
||||||
ensure that the next shape copy will be rendered in the correct
|
ensure that the next shape copy will be rendered in the correct
|
||||||
position.
|
position.
|
||||||
|
|
||||||
|
@ -161,13 +161,11 @@
|
|||||||
|
|
||||||
\snippet painting/transformations/renderarea.cpp 6
|
\snippet painting/transformations/renderarea.cpp 6
|
||||||
|
|
||||||
Before we start to render the shape, we call the QPainter::save()
|
Before we start to render the shape, we instantiate
|
||||||
function.
|
a QPainterStateGuard to save the current painter state (i.e. push the
|
||||||
|
state onto a stack) including the current coordinate system while
|
||||||
QPainter::save() saves the current painter state (i.e. pushes the
|
in scope. The rationale for saving the painter state is that the
|
||||||
state onto a stack) including the current coordinate system. The
|
following call to the \c transformPainter() function will transform 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
|
coordinate system depending on the currently chosen transformation
|
||||||
operations, and we need a way to get back to the original state to
|
operations, and we need a way to get back to the original state to
|
||||||
draw the outline.
|
draw the outline.
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <QLayout>
|
#include <QLayout>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QPainterStateGuard>
|
||||||
#include <QPainterPath>
|
#include <QPainterPath>
|
||||||
|
|
||||||
const int alpha = 155;
|
const int alpha = 155;
|
||||||
@ -76,7 +77,7 @@ void XFormView::resizeEvent(QResizeEvent *e)
|
|||||||
|
|
||||||
void XFormView::paint(QPainter *p)
|
void XFormView::paint(QPainter *p)
|
||||||
{
|
{
|
||||||
p->save();
|
QPainterStateGuard guard(p);
|
||||||
p->setRenderHint(QPainter::Antialiasing);
|
p->setRenderHint(QPainter::Antialiasing);
|
||||||
p->setRenderHint(QPainter::SmoothPixmapTransform);
|
p->setRenderHint(QPainter::SmoothPixmapTransform);
|
||||||
switch (m_type) {
|
switch (m_type) {
|
||||||
@ -90,7 +91,6 @@ void XFormView::paint(QPainter *p)
|
|||||||
drawTextType(p);
|
drawTextType(p);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
p->restore();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void XFormView::updateControlPoints(const QPolygonF &points)
|
void XFormView::updateControlPoints(const QPolygonF &points)
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPainterPath>
|
#include <QPainterPath>
|
||||||
|
#include <QPainterStateGuard>
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
RenderArea::RenderArea(QWidget *parent)
|
RenderArea::RenderArea(QWidget *parent)
|
||||||
@ -106,7 +107,7 @@ void RenderArea::paintEvent(QPaintEvent * /* event */)
|
|||||||
//! [10]
|
//! [10]
|
||||||
for (int x = 0; x < width(); x += 100) {
|
for (int x = 0; x < width(); x += 100) {
|
||||||
for (int y = 0; y < height(); y += 100) {
|
for (int y = 0; y < height(); y += 100) {
|
||||||
painter.save();
|
QPainterStateGuard guard(&painter);
|
||||||
painter.translate(x, y);
|
painter.translate(x, y);
|
||||||
//! [10] //! [11]
|
//! [10] //! [11]
|
||||||
if (transformed) {
|
if (transformed) {
|
||||||
@ -161,7 +162,6 @@ void RenderArea::paintEvent(QPaintEvent * /* event */)
|
|||||||
painter.drawPixmap(10, 10, pixmap);
|
painter.drawPixmap(10, 10, pixmap);
|
||||||
}
|
}
|
||||||
//! [12] //! [13]
|
//! [12] //! [13]
|
||||||
painter.restore();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "renderarea.h"
|
#include "renderarea.h"
|
||||||
|
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QPainterStateGuard>
|
||||||
#include <QPaintEvent>
|
#include <QPaintEvent>
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
@ -61,10 +62,11 @@ void RenderArea::paintEvent(QPaintEvent *event)
|
|||||||
//! [5]
|
//! [5]
|
||||||
|
|
||||||
//! [6]
|
//! [6]
|
||||||
painter.save();
|
{
|
||||||
|
QPainterStateGuard guard(&painter);
|
||||||
transformPainter(painter);
|
transformPainter(painter);
|
||||||
drawShape(painter);
|
drawShape(painter);
|
||||||
painter.restore();
|
}
|
||||||
//! [6]
|
//! [6]
|
||||||
|
|
||||||
//! [7]
|
//! [7]
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "analogclock.h"
|
#include "analogclock.h"
|
||||||
|
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QPainterStateGuard>
|
||||||
#include <QTime>
|
#include <QTime>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
@ -78,12 +79,12 @@ void AnalogClock::paintEvent(QPaintEvent *)
|
|||||||
//! [16]
|
//! [16]
|
||||||
//! [18]
|
//! [18]
|
||||||
|
|
||||||
//! [17]
|
//! [19]
|
||||||
painter.save();
|
{
|
||||||
//! [17] //! [19]
|
QPainterStateGuard guard(&painter);
|
||||||
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
||||||
painter.drawConvexPolygon(hourHand, 4);
|
painter.drawConvexPolygon(hourHand, 4);
|
||||||
painter.restore();
|
}
|
||||||
//! [18] //! [19]
|
//! [18] //! [19]
|
||||||
|
|
||||||
//! [20]
|
//! [20]
|
||||||
@ -97,10 +98,11 @@ void AnalogClock::paintEvent(QPaintEvent *)
|
|||||||
painter.setBrush(minuteColor);
|
painter.setBrush(minuteColor);
|
||||||
|
|
||||||
//! [22]
|
//! [22]
|
||||||
painter.save();
|
{
|
||||||
|
QPainterStateGuard guard(&painter);
|
||||||
painter.rotate(6.0 * time.minute());
|
painter.rotate(6.0 * time.minute());
|
||||||
painter.drawConvexPolygon(minuteHand, 4);
|
painter.drawConvexPolygon(minuteHand, 4);
|
||||||
painter.restore();
|
}
|
||||||
//! [21] //! [22]
|
//! [21] //! [22]
|
||||||
|
|
||||||
|
|
||||||
@ -109,12 +111,13 @@ void AnalogClock::paintEvent(QPaintEvent *)
|
|||||||
//! [23]
|
//! [23]
|
||||||
|
|
||||||
//! [24]
|
//! [24]
|
||||||
painter.save();
|
{
|
||||||
|
QPainterStateGuard guard(&painter);
|
||||||
painter.rotate(6.0 * time.second());
|
painter.rotate(6.0 * time.second());
|
||||||
painter.drawConvexPolygon(secondsHand, 4);
|
painter.drawConvexPolygon(secondsHand, 4);
|
||||||
painter.drawEllipse(-3, -3, 6, 6);
|
painter.drawEllipse(-3, -3, 6, 6);
|
||||||
painter.drawEllipse(-5, -68, 10, 10);
|
painter.drawEllipse(-5, -68, 10, 10);
|
||||||
painter.restore();
|
}
|
||||||
//! [24]
|
//! [24]
|
||||||
|
|
||||||
//! [25]
|
//! [25]
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QPainterStateGuard>
|
||||||
#include <QTime>
|
#include <QTime>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
@ -93,10 +94,11 @@ void ShapedClock::paintEvent(QPaintEvent *)
|
|||||||
painter.setPen(Qt::NoPen);
|
painter.setPen(Qt::NoPen);
|
||||||
painter.setBrush(hourColor);
|
painter.setBrush(hourColor);
|
||||||
|
|
||||||
painter.save();
|
{
|
||||||
|
QPainterStateGuard guard(&painter);
|
||||||
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
||||||
painter.drawConvexPolygon(hourHand, 4);
|
painter.drawConvexPolygon(hourHand, 4);
|
||||||
painter.restore();
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 12; ++i) {
|
for (int i = 0; i < 12; ++i) {
|
||||||
painter.drawRect(73, -3, 16, 6);
|
painter.drawRect(73, -3, 16, 6);
|
||||||
@ -105,19 +107,21 @@ void ShapedClock::paintEvent(QPaintEvent *)
|
|||||||
|
|
||||||
painter.setBrush(minuteColor);
|
painter.setBrush(minuteColor);
|
||||||
|
|
||||||
painter.save();
|
{
|
||||||
|
QPainterStateGuard guard(&painter);
|
||||||
painter.rotate(6.0 * time.minute());
|
painter.rotate(6.0 * time.minute());
|
||||||
painter.drawConvexPolygon(minuteHand, 4);
|
painter.drawConvexPolygon(minuteHand, 4);
|
||||||
painter.restore();
|
}
|
||||||
|
|
||||||
painter.setBrush(secondsColor);
|
painter.setBrush(secondsColor);
|
||||||
|
|
||||||
painter.save();
|
{
|
||||||
|
QPainterStateGuard guard(&painter);
|
||||||
painter.rotate(6.0 * time.second());
|
painter.rotate(6.0 * time.second());
|
||||||
painter.drawConvexPolygon(secondsHand, 4);
|
painter.drawConvexPolygon(secondsHand, 4);
|
||||||
painter.drawEllipse(-3, -3, 6, 6);
|
painter.drawEllipse(-3, -3, 6, 6);
|
||||||
painter.drawEllipse(-5, -68, 10, 10);
|
painter.drawEllipse(-5, -68, 10, 10);
|
||||||
painter.restore();
|
}
|
||||||
|
|
||||||
painter.setPen(minuteColor);
|
painter.setPen(minuteColor);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user