1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example widgets/styles
|
---|
30 | \title Styles Example
|
---|
31 |
|
---|
32 | The Styles example illustrates how to create custom widget
|
---|
33 | drawing styles using Qt, and demonstrates Qt's predefined styles.
|
---|
34 |
|
---|
35 | \image styles-enabledwood.png Screenshot of the Styles example
|
---|
36 |
|
---|
37 | A style in Qt is a subclass of QStyle or of one of its
|
---|
38 | subclasses. Styles perform drawing on behalf of widgets. Qt
|
---|
39 | provides a whole range of predefined styles, either built into
|
---|
40 | the \l QtGui library or found in plugins. Custom styles are
|
---|
41 | usually created by subclassing one of Qt's existing style and
|
---|
42 | reimplementing a few virtual functions.
|
---|
43 |
|
---|
44 | In this example, the custom style is called \c NorwegianWoodStyle
|
---|
45 | and derives from QMotifStyle. Its main features are the wooden
|
---|
46 | textures used for filling most of the widgets and its round
|
---|
47 | buttons and comboboxes.
|
---|
48 |
|
---|
49 | To implement the style, we use some advanced features provided by
|
---|
50 | QPainter, such as \l{QPainter::Antialiasing}{antialiasing} (to
|
---|
51 | obtain smoother button edges), \l{QColor::alpha()}{alpha blending}
|
---|
52 | (to make the buttons appeared raised or sunken), and
|
---|
53 | \l{QPainterPath}{painter paths} (to fill the buttons and draw the
|
---|
54 | outline). We also use many features of QBrush and QPalette.
|
---|
55 |
|
---|
56 | The example consists of the following classes:
|
---|
57 |
|
---|
58 | \list
|
---|
59 | \o \c NorwegianWoodStyle inherits from QMotifStyle and implements
|
---|
60 | the Norwegian Wood style.
|
---|
61 | \o \c WidgetGallery is a \c QDialog subclass that shows the most
|
---|
62 | common widgets and allows the user to switch style
|
---|
63 | dynamically.
|
---|
64 | \endlist
|
---|
65 |
|
---|
66 | \section1 NorwegianWoodStyle Class Definition
|
---|
67 |
|
---|
68 | Here's the definition of the \c NorwegianWoodStyle class:
|
---|
69 |
|
---|
70 | \snippet examples/widgets/styles/norwegianwoodstyle.h 0
|
---|
71 |
|
---|
72 | The public functions are all declared in QStyle (QMotifStyle's
|
---|
73 | grandparent class) and reimplemented here to override the Motif
|
---|
74 | look and feel. The private functions are helper functions.
|
---|
75 |
|
---|
76 | \section1 NorwegianWoodStyle Class Implementation
|
---|
77 |
|
---|
78 | We will now review the implementation of the \c
|
---|
79 | NorwegianWoodStyle class.
|
---|
80 |
|
---|
81 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 0
|
---|
82 |
|
---|
83 | The \c polish() function is reimplemented from QStyle. It takes a
|
---|
84 | QPalette as a reference and adapts the palette to fit the style.
|
---|
85 | Most styles don't need to reimplement that function. The
|
---|
86 | Norwegian Wood style reimplements it to set a "wooden" palette.
|
---|
87 |
|
---|
88 | We start by defining a few \l{QColor}s that we'll need. Then we
|
---|
89 | load two PNG images. The \c : prefix in the file path indicates
|
---|
90 | that the PNG files are \l{The Qt Resource System}{embedded
|
---|
91 | resources}.
|
---|
92 |
|
---|
93 | \table
|
---|
94 | \row \o \inlineimage widgets/styles/images/woodbackground.png
|
---|
95 |
|
---|
96 | \o \bold{woodbackground.png}
|
---|
97 |
|
---|
98 | This texture is used as the background of most widgets.
|
---|
99 | The wood pattern is horizontal.
|
---|
100 |
|
---|
101 | \row \o \inlineimage widgets/styles/images/woodbutton.png
|
---|
102 |
|
---|
103 | \o \bold{woodbutton.png}
|
---|
104 |
|
---|
105 | This texture is used for filling push buttons and
|
---|
106 | comboboxes. The wood pattern is vertical and more reddish
|
---|
107 | than the texture used for the background.
|
---|
108 | \endtable
|
---|
109 |
|
---|
110 | The \c midImage variable is initialized to be the same as \c
|
---|
111 | buttonImage, but then we use a QPainter and fill it with a 25%
|
---|
112 | opaque black color (a black with an \l{QColor::alpha()}{alpha
|
---|
113 | channel} of 63). The result is a somewhat darker image than \c
|
---|
114 | buttonImage. This image will be used for filling buttons that the
|
---|
115 | user is holding down.
|
---|
116 |
|
---|
117 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 1
|
---|
118 |
|
---|
119 | We initialize the palette. Palettes have various
|
---|
120 | \l{QPalette::ColorRole}{color roles}, such as QPalette::Base
|
---|
121 | (used for filling text editors, item views, etc.), QPalette::Text
|
---|
122 | (used for foreground text), and QPalette::Background (used for
|
---|
123 | the background of most widgets). Each role has its own QBrush,
|
---|
124 | which usually is a plain color but can also be a brush pattern or
|
---|
125 | even a texture (a QPixmap).
|
---|
126 |
|
---|
127 | In addition to the roles, palettes have several
|
---|
128 | \l{QPalette::ColorGroup}{color groups}: active, disabled, and
|
---|
129 | inactive. The active color group is used for painting widgets in
|
---|
130 | the active window. The disabled group is used for disabled
|
---|
131 | widgets. The inactive group is used for all other widgets. Most
|
---|
132 | palettes have identical active and inactive groups, while the
|
---|
133 | disabled group uses darker shades.
|
---|
134 |
|
---|
135 | We initialize the QPalette object with a brown color. Qt
|
---|
136 | automatically derivates all color roles for all color groups from
|
---|
137 | that single color. We then override some of the default values. For
|
---|
138 | example, we use Qt::darkGreen instead of the default
|
---|
139 | (Qt::darkBlue) for the QPalette::Highlight role. The
|
---|
140 | QPalette::setBrush() overload that we use here sets the same
|
---|
141 | color or brush for all three color groups.
|
---|
142 |
|
---|
143 | The \c setTexture() function is a private function that sets the
|
---|
144 | texture for a certain color role, while preserving the existing
|
---|
145 | color in the QBrush. A QBrush can hold both a solid color and a
|
---|
146 | texture at the same time. The solid color is used for drawing
|
---|
147 | text and other graphical elements where textures don't look good.
|
---|
148 |
|
---|
149 | At the end, we set the brush for the disabled color group of the
|
---|
150 | palette. We use \c woodbackground.png as the texture for all
|
---|
151 | disabled widgets, including buttons, and use a darker color to
|
---|
152 | accompany the texture.
|
---|
153 |
|
---|
154 | \image styles-disabledwood.png The Norwegian Wood style with disabled widgets
|
---|
155 |
|
---|
156 | Let's move on to the other functions reimplemented from
|
---|
157 | QMotifStyle:
|
---|
158 |
|
---|
159 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 3
|
---|
160 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 4
|
---|
161 |
|
---|
162 | This QStyle::polish() overload is called once on every widget
|
---|
163 | drawn using the style. We reimplement it to set the Qt::WA_Hover
|
---|
164 | attribute on \l{QPushButton}s and \l{QComboBox}es. When this
|
---|
165 | attribute is set, Qt generates paint events when the mouse
|
---|
166 | pointer enters or leaves the widget. This makes it possible to
|
---|
167 | render push buttons and comboboxes differently when the mouse
|
---|
168 | pointer is over them.
|
---|
169 |
|
---|
170 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 5
|
---|
171 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 6
|
---|
172 |
|
---|
173 | This QStyle::unpolish() overload is called to undo any
|
---|
174 | modification done to the widget in \c polish(). For simplicity,
|
---|
175 | we assume that the flag wasn't set before \c polish() was called.
|
---|
176 | In an ideal world, we would remember the original state for each
|
---|
177 | widgets (e.g., using a QMap<QWidget *, bool>) and restore it in
|
---|
178 | \c unpolish().
|
---|
179 |
|
---|
180 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 7
|
---|
181 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 8
|
---|
182 |
|
---|
183 | The \l{QStyle::pixelMetric()}{pixelMetric()} function returns the
|
---|
|
---|