diff options
author | Volker Hilsheimer <[email protected]> | 2025-04-15 12:58:40 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <[email protected]> | 2025-04-17 11:34:09 +0000 |
commit | e2beffe6f55daa55a9307290fd53175af76c729a (patch) | |
tree | 944c5e0d39593db8ac20d2c543a05b60400f8d7b | |
parent | b93d9dc01a068d719e0f06b59b4b566cd6f00163 (diff) |
Widget effects DRY: replace manual pointer handling with unique_ptr
Instead of repeatedly resetting of the static pointers after (or before)
calling deleteLater(), use a unique_ptr with a custom deleter.
Since we use deleteLater(), nothing will happen if one of those widgets
does leak to the point where static objects are destroyed; we'd at most
call deleteLater() at this point, which won't do anything (if we enqueue
a DeferredDelete event at all, then it will never be processed).
Task-number: QTBUG-135976
Change-Id: I36a4780093eafd064dcb1a72696c1d9b21483b77
Reviewed-by: Axel Spoerl <[email protected]>
(cherry picked from commit b50a2761e735208eeb62042ecfaba7a76e580454)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
(cherry picked from commit c9ba8760deec4b2f5c245e9120d59b622b383ceb)
-rw-r--r-- | src/widgets/widgets/qeffects.cpp | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/widgets/widgets/qeffects.cpp b/src/widgets/widgets/qeffects.cpp index 74ac24a2b0a..50c09ec4daf 100644 --- a/src/widgets/widgets/qeffects.cpp +++ b/src/widgets/widgets/qeffects.cpp @@ -19,6 +19,15 @@ QT_BEGIN_NAMESPACE +struct DeleteLater +{ + void operator()(QObject *o) const + { + if (o) + o->deleteLater(); + } +}; + /* Internal class QAlphaWidget. @@ -58,7 +67,7 @@ private: QElapsedTimer checkTime; }; -static QAlphaWidget* q_blend = nullptr; +static std::unique_ptr<QAlphaWidget, DeleteLater> q_blend; /* Constructs a QAlphaWidget. @@ -227,8 +236,7 @@ void QAlphaWidget::render() anim.stop(); qApp->removeEventFilter(this); widget->setWindowOpacity(1); - q_blend = 0; - deleteLater(); + q_blend.reset(); } else { widget->setWindowOpacity(alpha); } @@ -248,8 +256,7 @@ void QAlphaWidget::render() lower(); } } - q_blend = nullptr; - deleteLater(); + q_blend.reset(); } else { alphaBlend(); pm = QPixmap::fromImage(mixedImage); @@ -341,7 +348,7 @@ private: QPixmap pm; }; -static QRollEffect* q_roll = nullptr; +static std::unique_ptr<QRollEffect, DeleteLater> q_roll; /* Construct a QRollEffect widget. @@ -513,8 +520,7 @@ void QRollEffect::scroll() lower(); } } - q_roll = nullptr; - deleteLater(); + q_roll.reset(); } } @@ -524,10 +530,7 @@ void QRollEffect::scroll() */ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time) { - if (q_roll) { - q_roll->deleteLater(); - q_roll = nullptr; - } + q_roll.reset(); if (!w) return; @@ -537,7 +540,7 @@ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time) Qt::WindowFlags flags = Qt::ToolTip; // those can be popups - they would steal the focus, but are disabled - q_roll = new QRollEffect(w, flags, orient); + q_roll.reset(new QRollEffect(w, flags, orient)); q_roll->run(time); } @@ -546,10 +549,7 @@ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time) */ void qFadeEffect(QWidget* w, int time) { - if (q_blend) { - q_blend->deleteLater(); - q_blend = nullptr; - } + q_blend.reset(); if (!w) return; @@ -560,7 +560,7 @@ void qFadeEffect(QWidget* w, int time) Qt::WindowFlags flags = Qt::ToolTip; // those can be popups - they would steal the focus, but are disabled - q_blend = new QAlphaWidget(w, flags); + q_blend.reset(new QAlphaWidget(w, flags)); q_blend->run(time); } |