|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /*! |
|
43 \example tools/styleplugin |
|
44 \title Style Plugin Example |
|
45 |
|
46 This example shows how to create a plugin that extends Qt with a new |
|
47 GUI look and feel. |
|
48 |
|
49 \image stylepluginexample.png |
|
50 |
|
51 On some platforms, the native style will prevent the button |
|
52 from having a red background. In this case, try to run the example |
|
53 in another style (e.g., plastique). |
|
54 |
|
55 A plugin in Qt is a class stored in a shared library that can be |
|
56 loaded by a QPluginLoader at run-time. When you create plugins in |
|
57 Qt, they either extend a Qt application or Qt itself. Writing a |
|
58 plugin that extends Qt itself is achieved by inheriting one of the |
|
59 plugin \l{Plugin Classes}{base classes}, reimplementing functions |
|
60 from that class, and adding a macro. In this example we extend Qt |
|
61 by adding a new GUI look and feel (i.e., making a new QStyle |
|
62 available). A high-level introduction to plugins is given in the |
|
63 plugin \l{How to Create Qt Plugins}{overview document}. |
|
64 |
|
65 Plugins that provide new styles inherit the QStylePlugin base |
|
66 class. Style plugins are loaded by Qt and made available through |
|
67 QStyleFactory; we will look at this later. We have implemented \c |
|
68 SimpleStylePlugin, which provides \c SimpleStyle. The new style |
|
69 inherits QWindowsStyle and contributes to widget styling by |
|
70 drawing button backgrounds in red - not a major contribution, but |
|
71 it still makes a new style. We test the plugin with \c |
|
72 StyleWindow, in which we display a QPushButton. |
|
73 |
|
74 The \c SimpleStyle and \c StyleWindow classes do not contain any |
|
75 plugin specific functionality and their implementations are |
|
76 trivial; we will therefore leap past them and head on to the \c |
|
77 SimpleStylePlugin and the \c main() function. After we have looked |
|
78 at that, we examine the plugin's profile. |
|
79 |
|
80 |
|
81 \section1 SimpleStylePlugin Class Definition |
|
82 |
|
83 \c SimpleStylePlugin inherits QStylePlugin and is the plugin |
|
84 class. |
|
85 |
|
86 \snippet examples/tools/styleplugin/plugin/simplestyleplugin.h 0 |
|
87 |
|
88 \c keys() returns a list of style names that this plugin can |
|
89 create, while \c create() takes such a string and returns the |
|
90 QStyle corresponding to the key. Both functions are pure virtual |
|
91 functions reimplemented from QStylePlugin. When an application |
|
92 requests an instance of the \c SimpleStyle style, which this |
|
93 plugin creates, Qt will create it with this plugin. |
|
94 |
|
95 |
|
96 \section1 SimpleStylePlugin Class Implementation |
|
97 |
|
98 Here is the implementation of \c keys(): |
|
99 |
|
100 \snippet examples/tools/styleplugin/plugin/simplestyleplugin.cpp 0 |
|
101 |
|
102 Since this plugin only supports one style, we return a QStringList |
|
103 with the class name of that style. |
|
104 |
|
105 Here is the \c create() function: |
|
106 |
|
107 \snippet examples/tools/styleplugin/plugin/simplestyleplugin.cpp 1 |
|
108 |
|
109 Note that the key for style plugins are case insensitive. |
|
110 The case sensitivity varies from plugin to plugin, so you need to |
|
111 check this when implementing new plugins. |
|
112 |
|
113 \section1 The \c main() function |
|
114 |
|
115 \snippet examples/tools/styleplugin/stylewindow/main.cpp 0 |
|
116 |
|
117 Qt loads the available style plugins when the QApplication object |
|
118 is initialized. The QStyleFactory class knows about all styles and |
|
119 produces them with \l{QStyleFactory::}{create()} (it is a |
|
120 wrapper around all the style plugins). |
|
121 |
|
122 \section1 The Simple Style Plugin Profile |
|
123 |
|
124 The \c SimpleStylePlugin lives in its own directory and have |
|
125 its own profile: |
|
126 |
|
127 \snippet examples/tools/styleplugin/plugin/plugin.pro 0 |
|
128 |
|
129 In the plugin profile we need to set the lib template as we are |
|
130 building a shared library instead of an executable. We must also |
|
131 set the config to plugin. We set the library to be stored in the |
|
132 styles folder under stylewindow because this is a path in which Qt |
|
133 will search for style plugins. |
|
134 |
|
135 \section1 Related articles and examples |
|
136 |
|
137 In addition to the plugin \l{How to Create Qt Plugins}{overview |
|
138 document}, we have other examples and articles that concern |
|
139 plugins. |
|
140 |
|
141 In the \l{Echo Plugin Example}{echo plugin example} we show how to |
|
142 implement plugins that extends Qt applications rather than Qt |
|
143 itself, which is the case with the style plugin of this example. |
|
144 The \l{Plug & Paint Example}{plug & paint} example shows how to |
|
145 implement a static plugin as well as being a more involved example |
|
146 on plugins that extend applications. |
|
147 */ |