QProxyStyle Class

The QProxyStyle class is a convenience class that simplifies dynamically overriding QStyle elements. More...

Header: #include <QProxyStyle>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
Inherits: QCommonStyle

Public Functions

QProxyStyle(const QString &key)
QProxyStyle(QStyle *style = nullptr)
virtual ~QProxyStyle()
QStyle *baseStyle() const
void setBaseStyle(QStyle *style)

Reimplemented Public Functions

virtual void drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = nullptr) const override
virtual void drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override
virtual void drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const override
virtual void drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole = QPalette::NoRole) const override
virtual void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override
virtual QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const override
virtual QStyle::SubControl hitTestComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, const QPoint &pos, const QWidget *widget = nullptr) const override
virtual QRect itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const override
virtual QRect itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const override
virtual int layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override
virtual int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override
virtual void polish(QWidget *widget) override
virtual void polish(QPalette &pal) override
virtual void polish(QApplication *app) override
virtual QSize sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const override
virtual QIcon standardIcon(QStyle::StandardPixmap standardIcon, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override
virtual QPalette standardPalette() const override
virtual QPixmap standardPixmap(QStyle::StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget = nullptr) const override
virtual int styleHint(QStyle::StyleHint hint, const QStyleOption *option = nullptr, const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
virtual QRect subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *option, QStyle::SubControl sc, const QWidget *widget) const override
virtual QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const override
virtual void unpolish(QWidget *widget) override
virtual void unpolish(QApplication *app) override

Reimplemented Protected Functions

virtual bool event(QEvent *e) override

Detailed Description

A QProxyStyle wraps a QStyle (usually the default system style) for the purpose of dynamically overriding painting or other specific style behavior.

The following example shows how to override the shortcut underline behavior on any platform:

 #include "textedit.h"
 #include <QApplication>
 #include <QProxyStyle>

 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 0;
         return QProxyStyle::styleHint(hint, option, widget, returnData);
     }
 };

 int main(int argc, char **argv)
 {
     Q_INIT_RESOURCE(textedit);

     QApplication a(argc, argv);
     a.setStyle(new MyProxyStyle);
     TextEdit mw;
     mw.resize(700, 800);
     mw.show();
     //...
 }

Warning: The common styles provided by Qt will respect this hint, because they call QStyle::proxy(), but there is no guarantee that QStyle::proxy() will be called for user defined 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:

 ...
 auto proxy = new MyProxyStyle(QApplication::style()->name());
 proxy->setParent(widget);   // take ownership to avoid memleak
 widget->setStyle(proxy);
 ...

See also QStyle.

Member Function Documentation

QProxyStyle::QProxyStyle(const QString &key)

Constructs a QProxyStyle object for overriding behavior in the base style specified by style key, or in the current application style if the specified style key is unrecognized.

See also QStyleFactory::create().

QProxyStyle::QProxyStyle(QStyle *style = nullptr)

Constructs a QProxyStyle object for overriding behavior in the specified style, or in the default native style if style is not specified.

Ownership of style is transferred to QProxyStyle.

[virtual] QProxyStyle::~QProxyStyle()

Destroys the QProxyStyle object.

QStyle *QProxyStyle::baseStyle() const

Returns the proxy base style object. If no base style is set on the proxy style, QProxyStyle will create an instance of the application style instead.

See also setBaseStyle() and QStyle.

[override virtual] void QProxyStyle::drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = nullptr) const

Reimplements: QCommonStyle::drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const.

[override virtual] void QProxyStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const

Reimplements: QCommonStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *widget) const.

[override virtual] void QProxyStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const

Reimplements: QStyle::drawItemPixmap(QPainter *painter, const QRect &rectangle, int alignment, const QPixmap &pixmap) const.

[override virtual] void QProxyStyle::drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole = QPalette::NoRole) const

Reimplements: QStyle::drawItemText(QPainter *painter, const QRect &rectangle, int alignment, const QPalette &palette, bool enabled, const QString &text, QPalette::ColorRole textRole) const.

[override virtual] void QProxyStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const

Reimplements: QCommonStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *widget) const.

[override virtual protected] bool QProxyStyle::event(QEvent *e)

Reimplements: QObject::event(QEvent *e).

[override virtual] QPixmap QProxyStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const

Reimplements: QCommonStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const.

[override virtual] QStyle::SubControl QProxyStyle::hitTestComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, const QPoint &pos, const QWidget *widget = nullptr) const

Reimplements: QCommonStyle::hitTestComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, const QPoint &pt, const QWidget *widget) const.

[override virtual] QRect QProxyStyle::itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const

Reimplements: QStyle::itemPixmapRect(const QRect &rectangle, int alignment, const QPixmap &pixmap) const.

[override virtual] QRect QProxyStyle::itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const

Reimplements: QStyle::itemTextRect(const QFontMetrics &metrics, const QRect &rectangle, int alignment, bool enabled, const QString &text) const.

[override virtual] int QProxyStyle::layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const

Reimplements: QCommonStyle::layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, const QStyleOption *option, const QWidget *widget) const.

This slot is called by layoutSpacing() to determine the spacing that should be used between control1 and control2 in a layout. orientation specifies whether the controls are laid out side by side or stacked vertically. The option parameter can be used to pass extra information about the parent widget. The widget parameter is optional and can also be used if option is nullptr.

The default implementation returns -1.

See also combinedLayoutSpacing().

[override virtual] int QProxyStyle::pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const

Reimplements: QCommonStyle::pixelMetric(QStyle::PixelMetric m, const QStyleOption *opt, const QWidget *widget) const.

[override virtual] void QProxyStyle::polish(QWidget *widget)

Reimplements: QCommonStyle::polish(QWidget *widget).

[override virtual] void QProxyStyle::polish(QPalette &pal)

Reimplements: QCommonStyle::polish(QPalette &pal).

[override virtual] void QProxyStyle::polish(QApplication *app)

Reimplements: QCommonStyle::polish(QApplication *app).

void QProxyStyle::setBaseStyle(QStyle *style)

Sets the base style that should be proxied.

Ownership of style is transferred to QProxyStyle.

If style is nullptr, a desktop-dependent style will be assigned automatically.

See also baseStyle().

[override virtual] QSize QProxyStyle::sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const

Reimplements: QCommonStyle::sizeFromContents(QStyle::ContentsType ct, const QStyleOption *opt, const QSize &csz, const QWidget *widget) const.

[override virtual] QIcon QProxyStyle::standardIcon(QStyle::StandardPixmap standardIcon, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const

Reimplements: QStyle::standardIcon(QStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const.

Returns an icon for the given standardIcon.

Reimplement this slot to provide your own icons in a QStyle subclass. The option argument can be used to pass extra information required to find the appropriate icon. The widget argument is optional and can also be used to help find the icon.

[override virtual] QPalette QProxyStyle::standardPalette() const

Reimplements: QStyle::standardPalette() const.

[override virtual] QPixmap QProxyStyle::standardPixmap(QStyle::StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget = nullptr) const

Reimplements: QCommonStyle::standardPixmap(QStyle::StandardPixmap sp, const QStyleOption *option, const QWidget *widget) const.

[override virtual] int QProxyStyle::styleHint(QStyle::StyleHint hint, const QStyleOption *option = nullptr, const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const

Reimplements: QCommonStyle::styleHint(QStyle::StyleHint sh, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *hret) const.

[override virtual] QRect QProxyStyle::subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *option, QStyle::SubControl sc, const QWidget *widget) const

Reimplements: QCommonStyle::subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QStyle::SubControl sc, const QWidget *widget) const.

[override virtual] QRect QProxyStyle::subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const

Reimplements: QCommonStyle::subElementRect(QStyle::SubElement sr, const QStyleOption *opt, const QWidget *widget) const.

[override virtual] void QProxyStyle::unpolish(QWidget *widget)

Reimplements: QCommonStyle::unpolish(QWidget *widget).

[override virtual] void QProxyStyle::unpolish(QApplication *app)

Reimplements: QCommonStyle::unpolish(QApplication *application).