summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <[email protected]>2025-06-24 16:27:18 +0200
committerMarc Mutz <[email protected]>2025-07-04 15:31:18 +0200
commit31b846ffbf12cdbd70ffecf61c63c6215a48f605 (patch)
tree143d25166cd505dc375699288e1063f580ac70e9
parent1228575926383b02479130a68989c4571a5c1ef4 (diff)
QPainter: overload setPen/setBrush for rvalue argsHEADdev
QPainter is highly optimized, to the point that the getters are returning by cref and not by value. So overload the setter for the common case of users passing temporaries in order to save the atomic ref/deref. This does not alleviate the need for the existing (QColor), (Qt::PenStyle), etc overloads, because those avoid even the allocation of a QPen/BrushPrivate in certain cases, and they don't need to checkEmulation() (whatever that is). That's why we don't reimplement them to call the new rvalue overloads, even though that would DRY the code somewhat. There are other setters which one might consider giving rvalue overloads. Here's why I didn't: - The various QTransform getters return by cref, too, but move = copy for QTransform, so a setter rvalue overload wouldn't change anything. - The QFont getter also returns by cref, but the setter is resolving fonts internally, and those operations are not optimized for rvalues, so it would be premature to add this. Filed QTBUG-138025 to keep track. [ChangeLog][QtGui][QPainter] Added overloads of setPen() and setBrush() that take rvalues. Change-Id: Ie3cf16e7fa64c81d217eb396a6fdde4c7d6f066b Reviewed-by: Volker Hilsheimer <[email protected]>
-rw-r--r--src/gui/compat/removed_api.cpp2
-rw-r--r--src/gui/painting/qpainter.cpp28
-rw-r--r--src/gui/painting/qpainter.h22
3 files changed, 47 insertions, 5 deletions
diff --git a/src/gui/compat/removed_api.cpp b/src/gui/compat/removed_api.cpp
index 21ddea967b3..33549a06504 100644
--- a/src/gui/compat/removed_api.cpp
+++ b/src/gui/compat/removed_api.cpp
@@ -91,6 +91,8 @@ void QWindowSystemInterface::handleContextMenuEvent(QWindow *window, bool mouseT
#if QT_GUI_REMOVED_SINCE(6, 11)
+#include "qpainter.h" // inlined API
+
#include "qquaternion.h" // inlined API
diff --git a/src/gui/painting/qpainter.cpp b/src/gui/painting/qpainter.cpp
index 5013e96f740..344bb8f1bef 100644
--- a/src/gui/painting/qpainter.cpp
+++ b/src/gui/painting/qpainter.cpp
@@ -41,6 +41,8 @@
#include <private/qrawfont_p.h>
#include <private/qfont_p.h>
+#include <QtCore/private/qtclasshelper_p.h>
+
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@@ -3652,6 +3654,8 @@ void QPainter::setPen(const QColor &color)
}
/*!
+ \fn void QPainter::setPen(const QPen &pen)
+
Sets the painter's pen to be the given \a pen.
The \a pen defines how to draw lines and outlines, and it also
@@ -3660,7 +3664,13 @@ void QPainter::setPen(const QColor &color)
\sa pen(), {QPainter#Settings}{Settings}
*/
-void QPainter::setPen(const QPen &pen)
+/*!
+ \fn void QPainter::setPen(QPen &&pen)
+ \since 6.11
+ \overload
+*/
+
+void QPainter::doSetPen(const QPen &pen, QPen *rvalue)
{
#ifdef QT_DEBUG_DRAW
@@ -3677,7 +3687,7 @@ void QPainter::setPen(const QPen &pen)
if (d->state->pen == pen)
return;
- d->state->pen = pen;
+ q_choose_assign(d->state->pen, pen, rvalue);
if (d->extended) {
d->checkEmulation();
@@ -3733,6 +3743,8 @@ const QPen &QPainter::pen() const
/*!
+ \fn void QPainter::setBrush(const QBrush &brush)
+
Sets the painter's brush to the given \a brush.
The painter's brush defines how shapes are filled.
@@ -3740,7 +3752,13 @@ const QPen &QPainter::pen() const
\sa brush(), {QPainter#Settings}{Settings}
*/
-void QPainter::setBrush(const QBrush &brush)
+/*!
+ \fn void QPainter::setBrush(QBrush &&brush)
+ \since 6.11
+ \overload
+*/
+
+void QPainter::doSetBrush(const QBrush &brush, QBrush *rvalue)
{
#ifdef QT_DEBUG_DRAW
if constexpr (qt_show_painter_debug_output)
@@ -3756,13 +3774,13 @@ void QPainter::setBrush(const QBrush &brush)
return;
if (d->extended) {
- d->state->brush = brush;
+ q_choose_assign(d->state->brush, brush, rvalue);
d->checkEmulation();
d->extended->brushChanged();
return;
}
- d->state->brush = brush;
+ q_choose_assign(d->state->brush, brush, rvalue);
d->state->dirtyFlags |= QPaintEngine::DirtyBrush;
}
diff --git a/src/gui/painting/qpainter.h b/src/gui/painting/qpainter.h
index 3c09b632364..83bde3b8cad 100644
--- a/src/gui/painting/qpainter.h
+++ b/src/gui/painting/qpainter.h
@@ -150,11 +150,15 @@ public:
QFontInfo fontInfo() const;
void setPen(const QColor &color);
+ QT_GUI_INLINE_SINCE(6, 11)
void setPen(const QPen &pen);
+ void setPen(QPen &&pen) { doSetPen(pen, &pen); }
void setPen(Qt::PenStyle style);
const QPen &pen() const;
+ QT_GUI_INLINE_SINCE(6, 11)
void setBrush(const QBrush &brush);
+ void setBrush(QBrush &&brush) { doSetBrush(brush, &brush); }
void setBrush(Qt::BrushStyle style);
void setBrush(QColor color);
void setBrush(Qt::GlobalColor color) { setBrush(QColor(color)); }
@@ -419,6 +423,9 @@ public:
private:
Q_DISABLE_COPY(QPainter)
+ void doSetPen(const QPen &lvalue, QPen *rvalue);
+ void doSetBrush(const QBrush &lvalue, QBrush *rvalue);
+
std::unique_ptr<QPainterPrivate> d_ptr;
friend class QWidget;
@@ -700,6 +707,21 @@ inline void QPainter::fillRect(const QRectF &r, QGradient::Preset p)
fillRect(r, QGradient(p));
}
+#if QT_GUI_INLINE_IMPL_SINCE(6, 11)
+
+void QPainter::setPen(const QPen &p)
+{
+ doSetPen(p, nullptr);
+}
+
+void QPainter::setBrush(const QBrush &b)
+{
+ doSetBrush(b, nullptr);
+}
+
+#endif // QT_GUI_INLINE_IMPL_SINCE(6, 11)
+
+
inline void QPainter::setBrushOrigin(int x, int y)
{
setBrushOrigin(QPoint(x, y));