[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
|
---|
| |
---|