Examples: inherit NorwegianWoodStyle from QProxyStyle

The QStyle specializations are being made internal. The recommended way
to customize styles is now to use QProxyStyle (& QStyleFactory), or to
implement a full custom style one can alternatively subclass
QCommonStyle. The proxy style approach was chosen for this case, since
the example assumes some drawing functionality provided by the windows
style and it is not a "complete" custom style implementation but more
like a customization anyway.

Change-Id: Ib2477339cfef258cfc944a76a2eea728066e1f45
Reviewed-by: Geir Vattekar <geir.vattekar@digia.com>
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
bb10
J-P Nurmi 2012-11-26 13:24:12 +01:00 committed by The Qt Project
parent cd765bbae0
commit 5a0fd7fb04
3 changed files with 26 additions and 18 deletions

View File

@ -37,12 +37,15 @@
A style in Qt is a subclass of QStyle or of one of its
subclasses. Styles perform drawing on behalf of widgets. Qt
provides a whole range of predefined styles, either built into
the \l QtGui library or found in plugins. Custom styles are
usually created by subclassing one of Qt's existing style and
reimplementing a few virtual functions.
the \l QtWidgets library or found in plugins. Styles are usually
customized by subclassing QProxyStyle and reimplementing a few
virtual functions. While QProxyStyle provides a transparent way
to customize either a specific style or the appropriate platform's
default style, Qt also provides QCommonStyle as a convenient base
for full custom style implementations.
In this example, the custom style is called \c NorwegianWoodStyle
and derives from QWindowsStyle. Its main features are the wooden
and derives from QProxyStyle. Its main features are the wooden
textures used for filling most of the widgets and its round
buttons and comboboxes.
@ -56,7 +59,7 @@
The example consists of the following classes:
\list
\li \c NorwegianWoodStyle inherits from QWindowsStyle and implements
\li \c NorwegianWoodStyle inherits from QProxyStyle and implements
the Norwegian Wood style.
\li \c WidgetGallery is a \c QDialog subclass that shows the most
common widgets and allows the user to switch style
@ -69,7 +72,7 @@
\snippet widgets/styles/norwegianwoodstyle.h 0
The public functions are all declared in QStyle (QWindowsStyle's
The public functions are all declared in QStyle (QProxyStyle's
grandparent class) and reimplemented here to override the Windows
look and feel. The private functions are helper functions.
@ -154,7 +157,7 @@
\image styles-disabledwood.png The Norwegian Wood style with disabled widgets
Let's move on to the other functions reimplemented from
QWindowsStyle:
QProxyStyle:
\snippet widgets/styles/norwegianwoodstyle.cpp 3
\snippet widgets/styles/norwegianwoodstyle.cpp 4
@ -201,13 +204,13 @@
\snippet widgets/styles/norwegianwoodstyle.cpp 10
The \l{QStyle::styleHint()}{styleHint()} function returns some
hints to widgets or to the base style (in our case QWindowsStyle)
hints to widgets or to the base style (in our case QProxyStyle)
about how to draw the widgets. The Windows style returns \c true
for the QStyle::SH_DitherDisabledText hint, resulting in a most
unpleasing visual effect. We override this behavior and return \c
false instead. We also return \c true for the
QStyle::SH_EtchDisabledText hint, meaning that disabled text is
rendered with an embossed look (as QWindowsStyle does).
rendered with an embossed look.
\snippet widgets/styles/norwegianwoodstyle.cpp 11
\snippet widgets/styles/norwegianwoodstyle.cpp 12

View File

@ -42,6 +42,11 @@
#include "norwegianwoodstyle.h"
NorwegianWoodStyle::NorwegianWoodStyle() :
QProxyStyle(QStyleFactory::create("windows"))
{
}
//! [0]
void NorwegianWoodStyle::polish(QPalette &palette)
{
@ -112,9 +117,9 @@ int NorwegianWoodStyle::pixelMetric(PixelMetric metric,
case PM_ComboBoxFrameWidth:
return 8;
case PM_ScrollBarExtent:
return QWindowsStyle::pixelMetric(metric, option, widget) + 4;
return QProxyStyle::pixelMetric(metric, option, widget) + 4;
default:
return QWindowsStyle::pixelMetric(metric, option, widget);
return QProxyStyle::pixelMetric(metric, option, widget);
}
}
//! [8]
@ -131,7 +136,7 @@ int NorwegianWoodStyle::styleHint(StyleHint hint, const QStyleOption *option,
case SH_EtchDisabledText:
return int(true);
default:
return QWindowsStyle::styleHint(hint, option, widget, returnData);
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
}
//! [10]
@ -256,7 +261,7 @@ void NorwegianWoodStyle::drawPrimitive(PrimitiveElement element,
//! [32] //! [33]
default:
//! [33] //! [34]
QWindowsStyle::drawPrimitive(element, option, painter, widget);
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
}
//! [34]
@ -284,11 +289,11 @@ void NorwegianWoodStyle::drawControl(ControlElement element,
}
}
}
QWindowsStyle::drawControl(element, &myButtonOption, painter, widget);
QProxyStyle::drawControl(element, &myButtonOption, painter, widget);
}
break;
default:
QWindowsStyle::drawControl(element, option, painter, widget);
QProxyStyle::drawControl(element, option, painter, widget);
}
}
//! [36]

View File

@ -41,7 +41,7 @@
#ifndef NORWEGIANWOODSTYLE_H
#define NORWEGIANWOODSTYLE_H
#include <QWindowsStyle>
#include <QProxyStyle>
#include <QPalette>
QT_BEGIN_NAMESPACE
@ -49,12 +49,12 @@ class QPainterPath;
QT_END_NAMESPACE
//! [0]
class NorwegianWoodStyle : public QWindowsStyle
class NorwegianWoodStyle : public QProxyStyle
{
Q_OBJECT
public:
NorwegianWoodStyle() {}
NorwegianWoodStyle();
void polish(QPalette &palette);
void polish(QWidget *widget);