summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Ehrlicher <[email protected]>2025-06-18 21:24:21 +0200
committerChristian Ehrlicher <[email protected]>2025-06-21 15:52:04 +0200
commite97a1fdad3d2d294ad9bf2e2bcc5f818d9050910 (patch)
tree15bb1aa6721a688e6f2c41a8279d4e9f8c90d8af
parentc0d8ed882fe09dd0e52a5847b45a6e2fc3a7fb87 (diff)
Avoid using QPainter::brushOrigin() to save and restore the origin
Avoid QPainter::brushOrigin() since it returns a QPoint instead QPointF so it might loose precision. Use QPainterStateGuard instead to make sure the correct QPainter state is restored later on. Pick-to: 6.10 Task-number: QTBUG-137885 Change-Id: I3f49c72c0425e22ffb1246b42d926e704dc9d8dd Reviewed-by: Eirik Aavitsland <[email protected]>
-rw-r--r--src/plugins/styles/modernwindows/qwindowsvistastyle.cpp4
-rw-r--r--src/widgets/itemviews/qheaderview.cpp6
-rw-r--r--src/widgets/itemviews/qitemdelegate.cpp4
-rw-r--r--src/widgets/itemviews/qtreeview.cpp8
-rw-r--r--src/widgets/styles/qcommonstyle.cpp3
5 files changed, 13 insertions, 12 deletions
diff --git a/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp b/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp
index 7bda5e91b6e..0301b770a1e 100644
--- a/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp
+++ b/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp
@@ -5,6 +5,7 @@
#include "qwindowsvistastyle_p_p.h"
#include "qwindowsvistaanimation_p.h"
#include <qoperatingsystemversion.h>
+#include <qpainterstateguard.h>
#include <qscreen.h>
#include <qstylehints.h>
#include <qwindow.h>
@@ -2114,10 +2115,9 @@ void QWindowsVistaStyle::drawPrimitive(PrimitiveElement element, const QStyleOpt
QPixmap pixmap;
if (vopt->backgroundBrush.style() != Qt::NoBrush) {
- const QPointF oldBrushOrigin = painter->brushOrigin();
+ QPainterStateGuard psg(painter);
painter->setBrushOrigin(vopt->rect.topLeft());
painter->fillRect(vopt->rect, vopt->backgroundBrush);
- painter->setBrushOrigin(oldBrushOrigin);
}
if (hover || selected) {
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp
index 8a926b73002..b2fa08e406f 100644
--- a/src/widgets/itemviews/qheaderview.cpp
+++ b/src/widgets/itemviews/qheaderview.cpp
@@ -10,6 +10,7 @@
#include <qdebug.h>
#include <qevent.h>
#include <qpainter.h>
+#include <qpainterstateguard.h>
#include <qscrollbar.h>
#include <qstyle.h>
#include <qstyleoption.h>
@@ -3091,8 +3092,6 @@ void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logical
return;
QStyleOptionHeaderV2 opt;
- QPointF oldBO = painter->brushOrigin();
-
initStyleOption(&opt);
QBrush oBrushButton = opt.palette.brush(QPalette::Button);
@@ -3106,13 +3105,14 @@ void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logical
QBrush nBrushWindow = opt.palette.brush(QPalette::Window);
// If relevant brushes are not the same as from the regular widgets we set the brush origin
+ QPainterStateGuard psg(painter, QPainterStateGuard::InitialState::NoSave);
if (oBrushButton != nBrushButton || oBrushWindow != nBrushWindow) {
+ psg.save();
painter->setBrushOrigin(opt.rect.topLeft());
}
// draw the section.
style()->drawControl(QStyle::CE_Header, &opt, painter, this);
- painter->setBrushOrigin(oldBO);
}
/*!
diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp
index ccc35fd807b..1816853db70 100644
--- a/src/widgets/itemviews/qitemdelegate.cpp
+++ b/src/widgets/itemviews/qitemdelegate.cpp
@@ -7,6 +7,7 @@
#include <qapplication.h>
#include <qbrush.h>
#include <qpainter.h>
+#include <qpainterstateguard.h>
#include <qpalette.h>
#include <qpoint.h>
#include <qrect.h>
@@ -775,10 +776,9 @@ void QItemDelegate::drawBackground(QPainter *painter,
} else {
QVariant value = index.data(Qt::BackgroundRole);
if (value.canConvert<QBrush>()) {
- QPointF oldBO = painter->brushOrigin();
+ QPainterStateGuard psg(painter);
painter->setBrushOrigin(option.rect.topLeft());
painter->fillRect(option.rect, qvariant_cast<QBrush>(value));
- painter->setBrushOrigin(oldBO);
}
}
}
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp
index 23588d4917c..b141d8c5b59 100644
--- a/src/widgets/itemviews/qtreeview.cpp
+++ b/src/widgets/itemviews/qtreeview.cpp
@@ -7,6 +7,7 @@
#include <qapplication.h>
#include <qscrollbar.h>
#include <qpainter.h>
+#include <qpainterstateguard.h>
#include <qstack.h>
#include <qstyle.h>
#include <qstyleoption.h>
@@ -1897,9 +1898,11 @@ void QTreeView::drawBranches(QPainter *painter, const QRect &rect,
extraFlags |= QStyle::State_Enabled;
if (hasFocus())
extraFlags |= QStyle::State_Active;
- QPoint oldBO = painter->brushOrigin();
- if (verticalScrollMode() == QAbstractItemView::ScrollPerPixel)
+ QPainterStateGuard psg(painter, QPainterStateGuard::InitialState::NoSave);
+ if (verticalScrollMode() == QAbstractItemView::ScrollPerPixel) {
+ psg.save();
painter->setBrushOrigin(QPoint(0, verticalOffset()));
+ }
if (d->alternatingColors) {
opt.features.setFlag(QStyleOptionViewItem::Alternate, d->current & 1);
@@ -1960,7 +1963,6 @@ void QTreeView::drawBranches(QPainter *painter, const QRect &rect,
current = ancestor;
ancestor = current.parent();
}
- painter->setBrushOrigin(oldBO);
}
/*!
diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp
index 7a95e590211..b5972411534 100644
--- a/src/widgets/styles/qcommonstyle.cpp
+++ b/src/widgets/styles/qcommonstyle.cpp
@@ -698,10 +698,9 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q
p->fillRect(vopt->rect, vopt->palette.brush(cg, QPalette::Highlight));
} else {
if (vopt->backgroundBrush.style() != Qt::NoBrush) {
- QPointF oldBO = p->brushOrigin();
+ QPainterStateGuard psg(p);
p->setBrushOrigin(vopt->rect.topLeft());
p->fillRect(vopt->rect, vopt->backgroundBrush);
- p->setBrushOrigin(oldBO);
}
if (vopt->state & QStyle::State_Selected) {