[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 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
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 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.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
[2] | 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
|
---|
| 184 | size in pixels for a certain user interface element. By
|
---|
| 185 | reimplementing this function, we can affect the way certain
|
---|
| 186 | widgets are drawn and their size hint. Here, we return 8 as the
|
---|
| 187 | width around a shown in a QComboBox, ensuring that there is
|
---|
| 188 | enough place around the text and the arrow for the Norwegian Wood
|
---|
| 189 | round corners. The default value for this setting in the Motif
|
---|
| 190 | style is 2.
|
---|
| 191 |
|
---|
| 192 | We also change the extent of \l{QScrollBar}s, i.e., the height
|
---|
| 193 | for a horizontal scroll bar and the width for a vertical scroll
|
---|
| 194 | bar, to be 4 pixels more than in the Motif style. This makes the
|
---|
| 195 | style a bit more distinctive.
|
---|
| 196 |
|
---|
| 197 | For all other QStyle::PixelMetric elements, we use the Motif
|
---|
| 198 | settings.
|
---|
| 199 |
|
---|
| 200 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 9
|
---|
| 201 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 10
|
---|
| 202 |
|
---|
| 203 | The \l{QStyle::styleHint()}{styleHint()} function returns some
|
---|
| 204 | hints to widgets or to the base style (in our case QMotifStyle)
|
---|
| 205 | about how to draw the widgets. The Motif style returns \c true
|
---|
| 206 | for the QStyle::SH_DitherDisabledText hint, resulting in a most
|
---|
| 207 | unpleasing visual effect. We override this behavior and return \c
|
---|
| 208 | false instead. We also return \c true for the
|
---|
| 209 | QStyle::SH_EtchDisabledText hint, meaning that disabled text is
|
---|
| 210 | rendered with an embossed look (as QWindowsStyle does).
|
---|
| 211 |
|
---|
| 212 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 11
|
---|
| 213 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 12
|
---|
| 214 |
|
---|
| 215 | The \l{QStyle::drawPrimitive()}{drawPrimitive()} function is
|
---|
| 216 | called by Qt widgets to draw various fundamental graphical
|
---|
| 217 | elements. Here we reimplement it to draw QPushButton and
|
---|
| 218 | QComboBox with round corners. The button part of these widgets is
|
---|
| 219 | drawn using the QStyle::PE_PanelButtonCommand primitive element.
|
---|
| 220 |
|
---|
| 221 | The \c option parameter, of type QStyleOption, contains
|
---|
| 222 | everything we need to know about the widget we want to draw on.
|
---|
| 223 | In particular, \c option->rect gives the rectangle within which
|
---|
| 224 | to draw the primitive element. The \c painter parameter is a
|
---|
| 225 | QPainter object that we can use to draw on the widget.
|
---|
| 226 |
|
---|
| 227 | The \c widget parameter is the widget itself. Normally, all the
|
---|
| 228 | information we need is available in \c option and \c painter, so
|
---|
| 229 | we don't need \c widget. We can use it to perform special
|
---|
| 230 | effects; for example, QMacStyle uses it to animate default
|
---|
| 231 | buttons. If you use it, be aware that the caller is allowed to
|
---|
| 232 | pass a null pointer.
|
---|
| 233 |
|
---|
| 234 | We start by defining three \l{QColor}s that we'll need later on.
|
---|
| 235 | We also put the x, y, width, and height components of the
|
---|
| 236 | widget's rectangle in local variables. The value used for the \c
|
---|
| 237 | semiTransparentWhite and for the \c semiTransparentBlack color's
|
---|
| 238 | alpha channel depends on whether the mouse cursor is over the
|
---|
| 239 | widget or not. Since we set the Qt::WA_Hover attribute on
|
---|
| 240 | \l{QPushButton}s and \l{QComboBox}es, we can rely on the
|
---|
| 241 | QStyle::State_MouseOver flag to be set when the mouse is over the
|
---|
| 242 | widget.
|
---|
| 243 |
|
---|
| 244 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 13
|
---|
| 245 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 14
|
---|
| 246 |
|
---|
| 247 | The \c roundRect variable is a QPainterPath. A QPainterPath is is
|
---|
| 248 | a vectorial specification of a shape. Any shape (rectangle,
|
---|
| 249 | ellipse, spline, etc.) or combination of shapes can be expressed
|
---|
| 250 | as a path. We will use \c roundRect both for filling the button
|
---|
| 251 | background with a wooden texture and for drawing the outline. The
|
---|
| 252 | \c roundRectPath() function is a private function; we will come
|
---|
| 253 | back to it later.
|
---|
| 254 |
|
---|
| 255 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 15
|
---|
| 256 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 16
|
---|
| 257 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 17
|
---|
| 258 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 18
|
---|
| 259 |
|
---|
| 260 | We define two variables, \c brush and \c darker, and initialize
|
---|
| 261 | them based on the state of the button:
|
---|
| 262 |
|
---|
| 263 | \list
|
---|
| 264 | \o If the button is a \l{QPushButton::flat}{flat button}, we use
|
---|
| 265 | the \l{QPalette::Background}{Background} brush. We set \c
|
---|
| 266 | darker to \c true if the button is
|
---|
| 267 | \l{QAbstractButton::down}{down} or
|
---|
| 268 | \l{QAbstractButton::checked}{checked}.
|
---|
| 269 | \o If the button is currently held down by the user or in the
|
---|
| 270 | \l{QAbstractButton::checked}{checked} state, we use the
|
---|
| 271 | \l{QPalette::Mid}{Mid} component of the palette. We set
|
---|
| 272 | \c darker to \c true if the button is
|
---|
| 273 | \l{QAbstractButton::checked}{checked}.
|
---|
| 274 | \o Otherwise, we use the \l{QPalette::Button}{Button} component
|
---|
| 275 | of the palette.
|
---|
| 276 | \endlist
|
---|
| 277 |
|
---|
| 278 | The screenshot below illustrates how \l{QPushButton}s are
|
---|
| 279 | rendered based on their state:
|
---|
| 280 |
|
---|
| 281 | \image styles-woodbuttons.png Norwegian Wood buttons in different states
|
---|
| 282 |
|
---|
| 283 | To discover whether the button is flat or not, we need to cast
|
---|
| 284 | the \c option parameter to QStyleOptionButton and check if the
|
---|
| 285 | \l{QStyleOptionButton::features}{features} member specifies the
|
---|
| 286 | QStyleOptionButton::Flat flag. The qstyleoption_cast() function
|
---|
| 287 | performs a dynamic cast; if \c option is not a
|
---|
| 288 | QStyleOptionButton, qstyleoption_cast() returns a null pointer.
|
---|
| 289 |
|
---|
| 290 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 19
|
---|
| 291 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 20
|
---|
| 292 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 21
|
---|
| 293 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 22
|
---|
| 294 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 23
|
---|
| 295 |
|
---|
| 296 | We turn on antialiasing on QPainter. Antialiasing is a technique
|
---|
| 297 | that reduces the visual distortion that occurs when the edges of
|
---|
| 298 | a shape are converted into pixels. For the Norwegian Wood style,
|
---|
| 299 | we use it to obtain smoother edges for the round buttons.
|
---|
| 300 |
|
---|
| 301 | \image styles-aliasing.png Norwegian wood buttons with and without antialiasing
|
---|
| 302 |
|
---|
| 303 | The first call to QPainter::fillPath() draws the background of
|
---|
| 304 | the button with a wooden texture. The second call to
|
---|
| 305 | \l{QPainter::fillPath()}{fillPath()} paints the same area with a
|
---|
| 306 | semi-transparent black color (a black color with an alpha channel
|
---|
| 307 | of 63) to make the area darker if \c darker is true.
|
---|
| 308 |
|
---|
| 309 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 24
|
---|
| 310 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 25
|
---|
| 311 |
|
---|
| 312 | Next, we draw the outline. The top-left half of the outline and
|
---|
| 313 | the bottom-right half of the outline are drawn using different
|
---|
| 314 | \l{QPen}s to produce a 3D effect. Normally, the top-left half of
|
---|
| 315 | the outline is drawn lighter whereas the bottom-right half is
|
---|
| 316 | drawn darker, but if the button is
|
---|
| 317 | \l{QAbstractButton::down}{down} or
|
---|
| 318 | \l{QAbstractButton::checked}{checked}, we invert the two
|
---|
| 319 | \l{QPen}s to give a sunken look to the button.
|
---|
| 320 |
|
---|
| 321 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 26
|
---|
| 322 |
|
---|
| 323 | We draw the top-left part of the outline by calling
|
---|
| 324 | QPainter::drawPath() with an appropriate
|
---|
| 325 | \l{QPainter::setClipRegion()}{clip region}. If the
|
---|
| 326 | \l{QStyleOption::direction}{layout direction} is right-to-left
|
---|
| 327 | instead of left-to-right, we swap the \c x1, \c x2, \c x3, and \c
|
---|
| 328 | x4 variables to obtain correct results. On right-to-left desktop,
|
---|
| 329 | the "light" comes from the top-right corner of the screen instead
|
---|
| 330 | of the top-left corner; raised and sunken widgets must be drawn
|
---|
| 331 | accordingly.
|
---|
| 332 |
|
---|
| 333 | The diagram below illustrates how 3D effects are drawn according
|
---|
| 334 | to the layout direction. The area in red on the diagram
|
---|
| 335 | corresponds to the \c topHalf polygon:
|
---|
| 336 |
|
---|
| 337 | \image styles-3d.png
|
---|
| 338 |
|
---|
| 339 | An easy way to test how a style looks in right-to-left mode is to
|
---|
| 340 | pass the \c -reverse command-line option to the application. This
|
---|
| 341 | option is recognized by the QApplication constructor.
|
---|
| 342 |
|
---|
| 343 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 32
|
---|
| 344 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 33
|
---|
| 345 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 34
|
---|
| 346 |
|
---|
| 347 | The bottom-right part of the outline is drawn in a similar
|
---|
| 348 | fashion. Then we draw a one-pixel wide outline around the entire
|
---|
| 349 | button, using the \l{QPalette::Foreground}{Foreground} component
|
---|
| 350 | of the QPalette.
|
---|
| 351 |
|
---|
| 352 | This completes the QStyle::PE_PanelButtonCommand case of the \c
|
---|
| 353 | switch statement. Other primitive elements are handled by the
|
---|
| 354 | base style. Let's now turn to the other \c NorwegianWoodStyle
|
---|
| 355 | member functions:
|
---|
| 356 |
|
---|
| 357 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 35
|
---|
| 358 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 36
|
---|
| 359 |
|
---|
| 360 | We reimplement QStyle::drawControl() to draw the text on a
|
---|
| 361 | QPushButton in a bright color when the button is
|
---|
| 362 | \l{QAbstractButton::down}{down} or
|
---|
| 363 | \l{QAbstractButton::checked}{checked}.
|
---|
| 364 |
|
---|
| 365 | If the \c option parameter points to a QStyleOptionButton object
|
---|
| 366 | (it normally should), we take a copy of the object and modify its
|
---|
| 367 | \l{QStyleOption::palette}{palette} member to make the
|
---|
| 368 | QPalette::ButtonText be the same as the QPalette::BrightText
|
---|
| 369 | component (unless the widget is disabled).
|
---|
| 370 |
|
---|
| 371 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 37
|
---|
| 372 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 38
|
---|
| 373 |
|
---|
| 374 | The \c setTexture() function is a private function that sets the
|
---|
| 375 | \l{QBrush::texture()}{texture} component of the \l{QBrush}es for
|
---|
| 376 | a certain \l{QPalette::ColorRole}{color role}, for all three
|
---|
| 377 | \l{QPalette::ColorGroup}{color groups} (active, disabled,
|
---|
| 378 | inactive). We used it to initialize the Norwegian Wood palette in
|
---|
| 379 | \c polish(QPalette &).
|
---|
| 380 |
|
---|
| 381 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 39
|
---|
| 382 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 40
|
---|
| 383 |
|
---|
| 384 | The \c roundRectPath() function is a private function that
|
---|
| 385 | constructs a QPainterPath object for round buttons. The path
|
---|
| 386 | consists of eight segments: four arc segments for the corners and
|
---|
| 387 | four lines for the sides.
|
---|
| 388 |
|
---|
| 389 | With around 250 lines of code, we have a fully functional custom
|
---|
| 390 | style based on one of the predefined styles. Custom styles can be
|
---|
| 391 | used to provide a distinct look to an application or family of
|
---|
| 392 | applications.
|
---|
| 393 |
|
---|
| 394 | \section1 WidgetGallery Class
|
---|
| 395 |
|
---|
| 396 | For completeness, we will quickly review the \c WidgetGallery
|
---|
| 397 | class, which contains the most common Qt widgets and allows the
|
---|
| 398 | user to change style dynamically. Here's the class definition:
|
---|
| 399 |
|
---|
| 400 | \snippet examples/widgets/styles/widgetgallery.h 0
|
---|
| 401 | \dots
|
---|
| 402 | \snippet examples/widgets/styles/widgetgallery.h 1
|
---|
| 403 |
|
---|
| 404 | Here's the \c WidgetGallery constructor:
|
---|
| 405 |
|
---|
| 406 | \snippet examples/widgets/styles/widgetgallery.cpp 0
|
---|
| 407 |
|
---|
| 408 | We start by creating child widgets. The \gui Style combobox is
|
---|
| 409 | initialized with all the styles known to QStyleFactory, in
|
---|
| 410 | addition to \c NorwegianWood. The \c create...() functions are
|
---|
| 411 | private functions that set up the various parts of the \c
|
---|
| 412 | WidgetGallery.
|
---|
| 413 |
|
---|
| 414 | \snippet examples/widgets/styles/widgetgallery.cpp 1
|
---|
| 415 | \snippet examples/widgets/styles/widgetgallery.cpp 2
|
---|
| 416 |
|
---|
| 417 | We connect the \gui Style combobox to the \c changeStyle()
|
---|
| 418 | private slot, the \gui{Use style's standard palette} check box to
|
---|
| 419 | the \c changePalette() slot, and the \gui{Disable widgets} check
|
---|
| 420 | box to the child widgets'
|
---|
| 421 | \l{QWidget::setDisabled()}{setDisabled()} slot.
|
---|
| 422 |
|
---|
| 423 | \snippet examples/widgets/styles/widgetgallery.cpp 3
|
---|
| 424 | \snippet examples/widgets/styles/widgetgallery.cpp 4
|
---|
| 425 |
|
---|
| 426 | Finally, we put the child widgets in layouts.
|
---|
| 427 |
|
---|
| 428 | \snippet examples/widgets/styles/widgetgallery.cpp 5
|
---|
| 429 | \snippet examples/widgets/styles/widgetgallery.cpp 6
|
---|
| 430 |
|
---|
| 431 | When the user changes the style in the combobox, we call
|
---|
| 432 | QApplication::setStyle() to dynamically change the style of the
|
---|
| 433 | application.
|
---|
| 434 |
|
---|
| 435 | \snippet examples/widgets/styles/widgetgallery.cpp 7
|
---|
| 436 | \snippet examples/widgets/styles/widgetgallery.cpp 8
|
---|
| 437 |
|
---|
| 438 | If the user turns the \gui{Use style's standard palette} on, the
|
---|
| 439 | current style's \l{QStyle::standardPalette()}{standard palette}
|
---|
| 440 | is used; otherwise, the system's default palette is honored.
|
---|
| 441 |
|
---|
| 442 | For the Norwegian Wood style, this makes no difference because we
|
---|
| 443 | always override the palette with our own palette in \c
|
---|
| 444 | NorwegianWoodStyle::polish().
|
---|
| 445 |
|
---|
| 446 | \snippet examples/widgets/styles/widgetgallery.cpp 9
|
---|
| 447 | \snippet examples/widgets/styles/widgetgallery.cpp 10
|
---|
| 448 |
|
---|
| 449 | The \c advanceProgressBar() slot is called at regular intervals
|
---|
| 450 | to advance the progress bar. Since we don't know how long the
|
---|
| 451 | user will keep the Styles application running, we use a
|
---|
| 452 | logarithmic formula: The closer the progress bar gets to 100%,
|
---|
| 453 | the slower it advances.
|
---|
| 454 |
|
---|
| 455 | We will review \c createProgressBar() in a moment.
|
---|
| 456 |
|
---|
| 457 | \snippet examples/widgets/styles/widgetgallery.cpp 11
|
---|
| 458 | \snippet examples/widgets/styles/widgetgallery.cpp 12
|
---|
| 459 |
|
---|
| 460 | The \c createTopLeftGroupBox() function creates the QGroupBox
|
---|
| 461 | that occupies the top-left corner of the \c WidgetGallery. We
|
---|
| 462 | skip the \c createTopRightGroupBox(), \c
|
---|
| 463 | createBottomLeftTabWidget(), and \c createBottomRightGroupBox()
|
---|
| 464 | functions, which are very similar.
|
---|
| 465 |
|
---|
| 466 | \snippet examples/widgets/styles/widgetgallery.cpp 13
|
---|
| 467 |
|
---|
| 468 | In \c createProgressBar(), we create a QProgressBar at the bottom
|
---|
| 469 | of the \c WidgetGallery and connect its
|
---|
| 470 | \l{QTimer::timeout()}{timeout()} signal to the \c
|
---|
| 471 | advanceProgressBar() slot.
|
---|
| 472 | */
|
---|