1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \page plugins-howto.html
|
---|
44 | \title How to Create Qt Plugins
|
---|
45 | \brief A guide to creating plugins to extend Qt applications and functionality provided by Qt.
|
---|
46 | \ingroup howto
|
---|
47 |
|
---|
48 | \keyword QT_DEBUG_PLUGINS
|
---|
49 | \keyword QT_NO_PLUGIN_CHECK
|
---|
50 |
|
---|
51 | Qt provides two APIs for creating plugins:
|
---|
52 |
|
---|
53 | \list
|
---|
54 | \o A higher-level API for writing extensions to Qt itself: custom database
|
---|
55 | drivers, image formats, text codecs, custom styles, etc.
|
---|
56 | \o A lower-level API for extending Qt applications.
|
---|
57 | \endlist
|
---|
58 |
|
---|
59 | For example, if you want to write a custom QStyle subclass and
|
---|
60 | have Qt applications load it dynamically, you would use the
|
---|
61 | higher-level API.
|
---|
62 |
|
---|
63 | Since the higher-level API is built on top of the lower-level API,
|
---|
64 | some issues are common to both.
|
---|
65 |
|
---|
66 | If you want to provide plugins for use with \QD, see the QtDesigner
|
---|
67 | module documentation.
|
---|
68 |
|
---|
69 | Topics:
|
---|
70 |
|
---|
71 | \tableofcontents
|
---|
72 |
|
---|
73 | \section1 The Higher-Level API: Writing Qt Extensions
|
---|
74 |
|
---|
75 | Writing a plugin that extends Qt itself is achieved by
|
---|
76 | subclassing the appropriate plugin base class, implementing a few
|
---|
77 | functions, and adding a macro.
|
---|
78 |
|
---|
79 | There are several plugin base classes. Derived plugins are stored
|
---|
80 | by default in sub-directories of the standard plugin directory. Qt
|
---|
81 | will not find plugins if they are not stored in the right
|
---|
82 | directory.
|
---|
83 |
|
---|
84 | \table
|
---|
85 | \header \o Base Class \o Directory Name \o Key Case Sensitivity
|
---|
86 | \row \o QAccessibleBridgePlugin \o \c accessiblebridge \o Case Sensitive
|
---|
87 | \row \o QAccessiblePlugin \o \c accessible \o Case Sensitive
|
---|
88 | \row \o QDecorationPlugin \o \c decorations \o Case Insensitive
|
---|
89 | \row \o QFontEnginePlugin \o \c fontengines \o Case Insensitive
|
---|
90 | \row \o QIconEnginePlugin \o \c iconengines \o Case Insensitive
|
---|
91 | \row \o QImageIOPlugin \o \c imageformats \o Case Sensitive
|
---|
92 | \row \o QInputContextPlugin \o \c inputmethods \o Case Sensitive
|
---|
93 | \row \o QKbdDriverPlugin \o \c kbddrivers \o Case Insensitive
|
---|
94 | \row \o QMouseDriverPlugin \o \c mousedrivers \o Case Insensitive
|
---|
95 | \row \o QPictureFormatPlugin \o \c pictureformats \o Case Sensitive
|
---|
96 | \row \o QScreenDriverPlugin \o \c gfxdrivers \o Case Insensitive
|
---|
97 | \row \o QScriptExtensionPlugin \o \c script \o Case Sensitive
|
---|
98 | \row \o QSqlDriverPlugin \o \c sqldrivers \o Case Sensitive
|
---|
99 | \row \o QStylePlugin \o \c styles \o Case Insensitive
|
---|
100 | \row \o QTextCodecPlugin \o \c codecs \o Case Sensitive
|
---|
101 | \endtable
|
---|
102 |
|
---|
103 | But where is the \c{plugins} directory? When the application
|
---|
104 | is run, Qt will first treat the application's executable directory
|
---|
105 | as the \c{pluginsbase}. For example if the application is in
|
---|
106 | \c{C:\Program Files\MyApp} and has a style plugin, Qt will look in
|
---|
107 | \c{C:\Program Files\MyApp\styles}. (See
|
---|
108 | QCoreApplication::applicationDirPath() for how to find out where
|
---|
109 | the application's executable is.) Qt will also look in the
|
---|
110 | directory specified by
|
---|
111 | QLibraryInfo::location(QLibraryInfo::PluginsPath), which typically
|
---|
112 | is located in \c QTDIR/plugins (where \c QTDIR is the directory
|
---|
113 | where Qt is installed). If you want Qt to look in additional
|
---|
114 | places you can add as many paths as you need with calls to
|
---|
115 | QCoreApplication::addLibraryPath(). And if you want to set your
|
---|
116 | own path or paths you can use QCoreApplication::setLibraryPaths().
|
---|
117 | You can also use a \c qt.conf file to override the hard-coded
|
---|
118 | paths that are compiled into the Qt library. For more information,
|
---|
119 | see the \l {Using qt.conf} documentation. Yet another possibility
|
---|
120 | is to set the \c QT_PLUGIN_PATH environment variable before running
|
---|
121 | the application. If set, Qt will look for plugins in the
|
---|
122 | paths (separated by the system path separator) specified in the variable.
|
---|
123 |
|
---|
124 | Suppose that you have a new style class called \c MyStyle that you
|
---|
125 | want to make available as a plugin. The required code is
|
---|
126 | straightforward, here is the class definition (\c
|
---|
127 | mystyleplugin.h):
|
---|
128 |
|
---|
129 | \snippet doc/src/snippets/code/doc_src_plugins-howto.qdoc 0
|
---|
130 |
|
---|
131 | Ensure that the class implementation is located in a \c .cpp file
|
---|
132 | (including the class definition):
|
---|
133 |
|
---|
134 | \snippet doc/src/snippets/code/doc_src_plugins-howto.qdoc 1
|
---|
135 |
|
---|
136 | (Note that QStylePlugin is case insensitive, and the lower-case
|
---|
137 | version of the key is used in our
|
---|
138 | \l{QStylePlugin::create()}{create()} implementation; most other
|
---|
139 | plugins are case sensitive.)
|
---|
140 |
|
---|
141 | For database drivers, image formats, text codecs, and most other
|
---|
142 | plugin types, no explicit object creation is required. Qt will
|
---|
143 | find and create them as required. Styles are an exception, since
|
---|
144 | you might want to set a style explicitly in code. To apply a
|
---|
145 | style, use code like this:
|
---|
146 |
|
---|
147 | \snippet doc/src/snippets/code/doc_src_plugins-howto.qdoc 2
|
---|
148 |
|
---|
149 | Some plugin classes require additional functions to be
|
---|
150 | implemented. See the class documentation for details of the
|
---|
151 | virtual functions that must be reimplemented for each type of
|
---|
152 | plugin.
|
---|
153 |
|
---|
154 | Qt applications automatically know which plugins are available,
|
---|
155 | because plugins are stored in the standard plugin subdirectories.
|
---|
156 | Because of this applications don't require any code to find and load
|
---|
157 | plugins, since Qt handles them automatically.
|
---|
158 |
|
---|
159 | The default directory for plugins is \c{QTDIR/plugins} (where \c
|
---|
160 | QTDIR is the directory where Qt is installed), with each type of
|
---|
161 | plugin in a subdirectory for that type, e.g. \c styles. If you
|
---|
162 | want your applications to use plugins and you don't want to use
|
---|
163 | the standard plugins path, have your installation process
|
---|
164 | determine the path you want to use for the plugins, and save the
|
---|
165 | path, e.g. using QSettings, for the application to read when it
|
---|
166 | runs. The application can then call
|
---|
167 | QCoreApplication::addLibraryPath() with this path and your
|
---|
168 | plugins will be available to the application. Note that the final
|
---|
169 | part of the path (e.g., \c styles) cannot be changed.
|
---|
170 |
|
---|
171 | The normal way to include a plugin with an application is either
|
---|
172 | to \l{Static Plugins}{compile it in with the application} or to
|
---|
173 | compile it into a dynamic library and use it like any other
|
---|
174 | library.
|
---|
175 |
|
---|
176 | If you want the plugin to be loadable then one approach is to
|
---|
177 | create a subdirectory under the application and place the plugin
|
---|
178 | in that directory. If you distribute any of the plugins that come
|
---|
179 | with Qt (the ones located in the \c plugins directory), you must
|
---|
180 | copy the sub-directory under \c plugins where the plugin is
|
---|
181 | located to your applications root folder (i.e., do not include the
|
---|
182 | \c plugins directory).
|
---|
183 |
|
---|
184 | For more information about deployment,
|
---|
185 | see the \l {Deploying Qt Applications} documentation.
|
---|
186 |
|
---|
187 | The \l{Style Plugin Example} shows how to implement a plugin
|
---|
188 | that extends the QStylePlugin base class.
|
---|
189 |
|
---|
190 | \section1 The Lower-Level API: Extending Qt Applications
|
---|
191 |
|
---|
192 | Not only Qt itself but also Qt application can be extended
|
---|
193 | through plugins. This requires the application to detect and load
|
---|
194 | plugins using QPluginLoader. In that context, plugins may provide
|
---|
195 | arbitrary functionality and are not limited to database drivers,
|
---|
196 | image formats, text codecs, styles, and the other types of plugin
|
---|
197 | that extend Qt's functionality.
|
---|
198 |
|
---|
199 | Making an application extensible through plugins involves the
|
---|
200 | following steps:
|
---|
201 |
|
---|
202 | \list 1
|
---|
203 | \o Define a set of interfaces (classes with only pure virtual
|
---|
204 | functions) used to talk to the plugins.
|
---|
205 | \o Use the Q_DECLARE_INTERFACE() macro to tell Qt's
|
---|
206 | \l{meta-object system} about the interface.
|
---|
207 | \o Use QPluginLoader in the application to load the plugins.
|
---|
208 | \o Use qobject_cast() to test whether a plugin implements a given
|
---|
209 | interface.
|
---|
210 | \endlist
|
---|
211 |
|
---|
212 | Writing a plugin involves these steps:
|
---|
213 |
|
---|
214 | \list 1
|
---|
215 | \o Declare a plugin class that inherits from QObject and from the
|
---|
216 | interfaces that the plugin wants to provide.
|
---|
217 | \o Use the Q_INTERFACES() macro to tell Qt's \l{meta-object
|
---|
218 | system} about the interfaces.
|
---|
219 | \o Export the plugin using the Q_EXPORT_PLUGIN2() macro.
|
---|
220 | \o Build the plugin using a suitable \c .pro file.
|
---|
|
---|