From 1bcfada9f04bd20a7a64d448fbf60c0f0820ed09 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Wed, 30 Dec 2020 13:55:27 +0100 Subject: QStyle: add name() to return the name of the style Currently there is no way to get the name of the current style to e.g. create a QProxyStyle for a specific widget only. Therefore add QStyle::name() so QProxyStyle(const QString &) can be called without hard-coding the style name. Remove an unused doc snippet as drive-by. Fixes: QTBUG-8004 Change-Id: I466c5e870a5392b238365bdc930f6a2ecee50cdb Reviewed-by: Richard Moe Gustavsen --- .../doc/snippets/code/src_gui_qproxystyle.cpp | 24 ++++++++-------------- src/widgets/styles/qproxystyle.cpp | 7 +++++++ src/widgets/styles/qstyle.cpp | 24 ++++++++++++++++++++++ src/widgets/styles/qstyle.h | 6 ++++++ src/widgets/styles/qstyle_p.h | 1 + src/widgets/styles/qstylefactory.cpp | 4 +++- 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/widgets/doc/snippets/code/src_gui_qproxystyle.cpp b/src/widgets/doc/snippets/code/src_gui_qproxystyle.cpp index 98dc0ff55b..636a0f52a8 100644 --- a/src/widgets/doc/snippets/code/src_gui_qproxystyle.cpp +++ b/src/widgets/doc/snippets/code/src_gui_qproxystyle.cpp @@ -48,22 +48,6 @@ ** ****************************************************************************/ -//! [0] -class MyProxyStyle : public QProxyStyle -{ -public: - - int styleHint(StyleHint hint, const QStyleOption *option = nullptr, - const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override - { - if (hint == QStyle::SH_UnderlineShortcut) - return 1; - return QProxyStyle::styleHint(hint, option, widget, returnData); - } -}; - -//! [0] - //! [1] #include "textedit.h" #include @@ -93,3 +77,11 @@ int main(int argc, char **argv) //... } //! [1] + +//! [2] +... +auto proxy = new MyProxyStyle(QApplication::style()->name()); +proxy->setParent(widget); // take ownership to avoid memleak +widget->setStyle(proxy); +... +//! [2] diff --git a/src/widgets/styles/qproxystyle.cpp b/src/widgets/styles/qproxystyle.cpp index ecad637957..31fe587ce2 100644 --- a/src/widgets/styles/qproxystyle.cpp +++ b/src/widgets/styles/qproxystyle.cpp @@ -72,6 +72,13 @@ QT_BEGIN_NAMESPACE or system controlled styles. It would not work on a Mac, for example, where menus are handled by the operating system. + When a proxy style should be set on a specific widget only, you have + to make sure to not set the proxy on the global application style which + is returned by QWidget::style(). You have to create a separate custom style + for the widget similar to: + + \snippet code/src_gui_qproxystyle.cpp 2 + \sa QStyle */ diff --git a/src/widgets/styles/qstyle.cpp b/src/widgets/styles/qstyle.cpp index 239d873649..9f49a55386 100644 --- a/src/widgets/styles/qstyle.cpp +++ b/src/widgets/styles/qstyle.cpp @@ -421,6 +421,30 @@ QStyle::~QStyle() { } +/*! + Returns the name of the style. + + This value can be used to create a style with QStyleFactory::create(). + + \sa QStyleFactory::create() + \since 6.1 +*/ +QString QStyle::name() const +{ + Q_D(const QStyle); + return d->name; +} + +/*! + \internal + Set the style name +*/ +void QStyle::setName(const QString &name) +{ + Q_D(QStyle); + d->name = name; +} + /*! Initializes the appearance of the given \a widget. diff --git a/src/widgets/styles/qstyle.h b/src/widgets/styles/qstyle.h index 0e3495306c..ec13fdb80d 100644 --- a/src/widgets/styles/qstyle.h +++ b/src/widgets/styles/qstyle.h @@ -73,6 +73,8 @@ public: QStyle(); virtual ~QStyle(); + QString name() const; + virtual void polish(QWidget *widget); virtual void unpolish(QWidget *widget); @@ -854,6 +856,9 @@ public: const QStyle * proxy() const; +private: + void setName(const QString &name); + private: Q_DISABLE_COPY(QStyle) friend class QWidget; @@ -861,6 +866,7 @@ private: friend class QApplication; friend class QProxyStyle; friend class QProxyStylePrivate; + friend class QStyleFactory; void setProxy(QStyle *style); }; diff --git a/src/widgets/styles/qstyle_p.h b/src/widgets/styles/qstyle_p.h index 7942cafaf7..ab451d4873 100644 --- a/src/widgets/styles/qstyle_p.h +++ b/src/widgets/styles/qstyle_p.h @@ -72,6 +72,7 @@ public: mutable int layoutSpacingIndex; QStyle *proxyStyle; + QString name; }; inline QImage styleCacheImage(const QSize &size) diff --git a/src/widgets/styles/qstylefactory.cpp b/src/widgets/styles/qstylefactory.cpp index 87a8000ce0..70d08468ca 100644 --- a/src/widgets/styles/qstylefactory.cpp +++ b/src/widgets/styles/qstylefactory.cpp @@ -109,8 +109,10 @@ QStyle *QStyleFactory::create(const QString& key) { } // Keep these here - they make the #ifdefery above work if (!ret) ret = qLoadPlugin(loader(), style); - if (ret) + if (ret) { ret->setObjectName(style); + ret->setName(style); + } return ret; } -- cgit v1.2.3