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 tools/styleplugin
|
---|
30 | \title Style Plugin Example
|
---|
31 |
|
---|
32 | This example shows how to create a plugin that extends Qt with a new
|
---|
33 | GUI look and feel.
|
---|
34 |
|
---|
35 | \image stylepluginexample.png
|
---|
36 |
|
---|
37 | On some platforms, the native style will prevent the button
|
---|
38 | from having a red background. In this case, try to run the example
|
---|
39 | in another style (e.g., plastique).
|
---|
40 |
|
---|
41 | A plugin in Qt is a class stored in a shared library that can be
|
---|
42 | loaded by a QPluginLoader at run-time. When you create plugins in
|
---|
43 | Qt, they either extend a Qt application or Qt itself. Writing a
|
---|
44 | plugin that extends Qt itself is achieved by inheriting one of the
|
---|
45 | plugin \l{Plugin Classes}{base classes}, reimplementing functions
|
---|
46 | from that class, and adding a macro. In this example we extend Qt
|
---|
47 | by adding a new GUI look and feel (i.e., making a new QStyle
|
---|
48 | available). A high-level introduction to plugins is given in the
|
---|
49 | plugin \l{How to Create Qt Plugins}{overview document}.
|
---|
50 |
|
---|
51 | Plugins that provide new styles inherit the QStylePlugin base
|
---|
52 | class. Style plugins are loaded by Qt and made available through
|
---|
53 | QStyleFactory; we will look at this later. We have implemented \c
|
---|
54 | SimpleStylePlugin, which provides \c SimpleStyle. The new style
|
---|
55 | inherits QWindowsStyle and contributes to widget styling by
|
---|
56 | drawing button backgrounds in red - not a major contribution, but
|
---|
57 | it still makes a new style. We test the plugin with \c
|
---|
58 | StyleWindow, in which we display a QPushButton.
|
---|
59 |
|
---|
60 | The \c SimpleStyle and \c StyleWindow classes do not contain any
|
---|
61 | plugin specific functionality and their implementations are
|
---|
62 | trivial; we will therefore leap past them and head on to the \c
|
---|
63 | SimpleStylePlugin and the \c main() function. After we have looked
|
---|
64 | at that, we examine the plugin's profile.
|
---|
65 |
|
---|
66 |
|
---|
67 | \section1 SimpleStylePlugin Class Definition
|
---|
68 |
|
---|
69 | \c SimpleStylePlugin inherits QStylePlugin and is the plugin
|
---|
70 | class.
|
---|
71 |
|
---|
72 | \snippet examples/tools/styleplugin/plugin/simplestyleplugin.h 0
|
---|
73 |
|
---|
74 | \c keys() returns a list of style names that this plugin can
|
---|
75 | create, while \c create() takes such a string and returns the
|
---|
76 | QStyle corresponding to the key. Both functions are pure virtual
|
---|
77 | functions reimplemented from QStylePlugin. When an application
|
---|
78 | requests an instance of the \c SimpleStyle style, which this
|
---|
79 | plugin creates, Qt will create it with this plugin.
|
---|
80 |
|
---|
81 |
|
---|
82 | \section1 SimpleStylePlugin Class Implementation
|
---|
83 |
|
---|
84 | Here is the implementation of \c keys():
|
---|
85 |
|
---|
86 | \snippet examples/tools/styleplugin/plugin/simplestyleplugin.cpp 0
|
---|
87 |
|
---|
88 | Since this plugin only supports one style, we return a QStringList
|
---|
89 | with the class name of that style.
|
---|
90 |
|
---|
91 | Here is the \c create() function:
|
---|
92 |
|
---|
93 | \snippet examples/tools/styleplugin/plugin/simplestyleplugin.cpp 1
|
---|
94 |
|
---|
95 | Note that the key for style plugins are case insensitive.
|
---|
96 | The case sensitivity varies from plugin to plugin, so you need to
|
---|
97 | check this when implementing new plugins.
|
---|
98 |
|
---|
99 | \section1 The \c main() function
|
---|
100 |
|
---|
101 | \snippet examples/tools/styleplugin/stylewindow/main.cpp 0
|
---|
102 |
|
---|
103 | Qt loads the available style plugins when the QApplication object
|
---|
104 | is initialized. The QStyleFactory class knows about all styles and
|
---|
105 | produces them with \l{QStyleFactory::}{create()} (it is a
|
---|
106 | wrapper around all the style plugins).
|
---|
107 |
|
---|
108 | \section1 The Simple Style Plugin Profile
|
---|
109 |
|
---|
110 | The \c SimpleStylePlugin lives in its own directory and have
|
---|
111 | its own profile:
|
---|
112 |
|
---|
113 | \snippet examples/tools/styleplugin/plugin/plugin.pro 0
|
---|
114 |
|
---|
115 | In the plugin profile we need to set the lib template as we are
|
---|
116 | building a shared library instead of an executable. We must also
|
---|
117 | set the config to plugin. We set the library to be stored in the
|
---|
118 | styles folder under stylewindow because this is a path in which Qt
|
---|
119 | will search for style plugins.
|
---|
120 |
|
---|
121 | \section1 Related articles and examples
|
---|
122 |
|
---|
123 | In addition to the plugin \l{How to Create Qt Plugins}{overview
|
---|
124 | document}, we have other examples and articles that concern
|
---|
125 | plugins.
|
---|
126 |
|
---|
127 | In the \l{Echo Plugin Example}{echo plugin example} we show how to
|
---|
128 | implement plugins that extends Qt applications rather than Qt
|
---|
129 | itself, which is the case with the style plugin of this example.
|
---|
130 | The \l{Plug & Paint Example}{plug & paint} example shows how to
|
---|
131 | implement a static plugin as well as being a more involved example
|
---|
132 | on plugins that extend applications.
|
---|
133 | */
|
---|