From eba3d84c88a5e856f4e1da61700f2af30be2f407 Mon Sep 17 00:00:00 2001 From: Tom Ransdell Date: Sun, 12 Jul 2020 22:16:33 -0400 Subject: [PATCH] Fix proxy style to support a proxy as a base style Add code to update any base styles to always use QStyle::setProxy(this). This allows composing new styles by chaining multiple style proxies together to build more complex styles. Previously the baseStyle had to be a normal QStyle class, usually one of the default platform styles: "windows", "macintosh", "fusion", etc. Change-Id: Ie7c2f66355d9b1fe3fe1f0904418f136229742e8 --- src/widgets/styles/qproxystyle.cpp | 19 +++++++++++++++++++ src/widgets/styles/qproxystyle.h | 1 + 2 files changed, 20 insertions(+) diff --git a/src/widgets/styles/qproxystyle.cpp b/src/widgets/styles/qproxystyle.cpp index 5739678932..d8157711da 100644 --- a/src/widgets/styles/qproxystyle.cpp +++ b/src/widgets/styles/qproxystyle.cpp @@ -105,6 +105,23 @@ void QProxyStylePrivate::ensureBaseStyle() const baseStyle->setParent(const_cast(q)); // Take ownership } +/* \internal + + This function updates the base style's proxy if the given style is also + a QProxyStyle. + + This function ensures each baseStyle::style() always returns the last proxy + so one can compose styles by chaining two or more proxy styles together + instead of requiring all the new style logic to be in a single proxy style. +*/ +void QProxyStyle::updateAnyBaseStyleProxies(QStyle *style) +{ + while (QProxyStyle *proxy = qobject_cast(style)) { + style = proxy->baseStyle(); + style->setProxy(this); + } +} + /*! Constructs a QProxyStyle object for overriding behavior in the specified \a style, or in the default native \l{QApplication::style()} @@ -120,6 +137,7 @@ QProxyStyle::QProxyStyle(QStyle *style) : d->baseStyle = style; style->setProxy(this); style->setParent(this); // Take ownership + updateAnyBaseStyleProxies(style); } } @@ -177,6 +195,7 @@ void QProxyStyle::setBaseStyle(QStyle *style) if (d->baseStyle) { d->baseStyle->setProxy(this); d->baseStyle->setParent(this); + updateAnyBaseStyleProxies(d->baseStyle); } } diff --git a/src/widgets/styles/qproxystyle.h b/src/widgets/styles/qproxystyle.h index c26d9abf59..0cffe7f703 100644 --- a/src/widgets/styles/qproxystyle.h +++ b/src/widgets/styles/qproxystyle.h @@ -99,6 +99,7 @@ protected: private: Q_DISABLE_COPY(QProxyStyle) Q_DECLARE_PRIVATE(QProxyStyle) + void updateAnyBaseStyleProxies(QStyle *style); }; #endif // QT_NO_STYLE_PROXY -- 2.23.0