~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/plugins-howto.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/plugins-howto.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: How to Create Qt Plugins</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">How to Create Qt Plugins</h1>
 
21
<p>Qt provides two APIs for creating plugins:</p>
 
22
<ul>
 
23
<li>A higher-level API for writing extensions to Qt itself: custom database drivers, image formats, text codecs, custom styles, etc.</li>
 
24
<li>A lower-lever API for extending Qt applications.</li>
 
25
</ul>
 
26
<p>For example, if you want to write a custom <a href="qstyle.html">QStyle</a> subclass and have Qt applications load it dynamically, you would then use the higher-level API; whereas if you want to extend <i>Qt Designer</i>, you would use the lower-level API.</p>
 
27
<p>Since the higher-level API is built on top of the lower-level API, some issues are common to both.</p>
 
28
<p>Topics:</p>
 
29
<ul><li><a href="#the-higher-level-api-writing-qt-extensions">The Higher-Level API: Writing Qt Extensions</a></li>
 
30
<li><a href="#the-lower-level-api-extending-qt-applications">The Lower-Level API: Extending Qt Applications</a></li>
 
31
<li><a href="#loading-and-verifying-plugins">Loading and Verifying Plugins</a></li>
 
32
<li><a href="#the-build-key">The Build Key</a></li>
 
33
</ul>
 
34
<a name="the-higher-level-api-writing-qt-extensions"></a>
 
35
<h2>The Higher-Level API: Writing Qt Extensions</h2>
 
36
<p>Writing a plugin that extends Qt itself is achieved by subclassing the appropriate plugin base clase, implementing a few functions, and adding a macro.</p>
 
37
<p>There are several plugin base classes. Derived plugins are stored by default in the standard plugin directory.</p>
 
38
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
39
<tr valign="top" bgcolor="#a2c511"><th>Base Class</th><th>Default Path</th><th>Key Case Sensitivity</th></tr>
 
40
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qaccessiblebridgeplugin.html">QAccessibleBridgePlugin</a></td><td><tt>plugins/accessiblebridge</tt></td><td>Case Sensitive</td></tr>
 
41
<tr valign="top" bgcolor="#e0e0e0"><td><a href="qaccessibleplugin.html">QAccessiblePlugin</a></td><td><tt>plugins/accessible</tt></td><td>Case Sensitive</td></tr>
 
42
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qdecorationplugin.html">QDecorationPlugin</a></td><td><tt>plugins/decorations</tt></td><td>Case Sensitive</td></tr>
 
43
<tr valign="top" bgcolor="#e0e0e0"><td><a href="qgfxdriverplugin.html">QGfxDriverPlugin</a></td><td><tt>plugins/gfxdrivers</tt></td><td>Case Sensitive</td></tr>
 
44
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qiconengineplugin.html">QIconEnginePlugin</a></td><td><tt>plugins/iconengines</tt></td><td>Case Insensitive</td></tr>
 
45
<tr valign="top" bgcolor="#e0e0e0"><td><a href="qimageioplugin.html">QImageIOPlugin</a></td><td><tt>plugins/imageformats</tt></td><td>Case Sensitive</td></tr>
 
46
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qinputcontextplugin.html">QInputContextPlugin</a></td><td><tt>plugins/inputmethods</tt></td><td>Case Sensitive</td></tr>
 
47
<tr valign="top" bgcolor="#e0e0e0"><td><a href="qkbddriverplugin.html">QKbdDriverPlugin</a></td><td><tt>plugins/kbddrivers</tt></td><td>Case Sensitive</td></tr>
 
48
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qmousedriverplugin.html">QMouseDriverPlugin</a></td><td><tt>plugins/mousedrivers</tt></td><td>Case Sensitive</td></tr>
 
49
<tr valign="top" bgcolor="#e0e0e0"><td><a href="qpictureformatplugin.html">QPictureFormatPlugin</a></td><td><tt>plugins/pictureformats</tt></td><td>Case Sensitive</td></tr>
 
50
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qsqldriverplugin.html">QSqlDriverPlugin</a></td><td><tt>plugins/sqldrivers</tt></td><td>Case Sensitive</td></tr>
 
51
<tr valign="top" bgcolor="#e0e0e0"><td><a href="qstyleplugin.html">QStylePlugin</a></td><td><tt>plugins/styles</tt></td><td>Case Insensitive</td></tr>
 
52
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qtextcodecplugin.html">QTextCodecPlugin</a></td><td><tt>plugins/codecs</tt></td><td>Case Sensitive</td></tr>
 
53
</table>
 
54
<p>But where is the <tt>plugins</tt> directory? When the application is run, Qt will first treat the application's executable directory as the <tt>pluginsbase</tt>. For example if the application is in <tt>C:\Program Files\MyApp</tt> and has a style plugin, Qt will look in <tt>C:\Program Files\MyApp\styles</tt>. (See <a href="qcoreapplication.html#applicationDirPath">QCoreApplication::applicationDirPath</a>() for how to find out where the application's executable is.) Qt will also look in the directory specified by QLibraryInfo::location(<a href="qlibraryinfo.html#LibraryLocation-enum">QLibraryInfo::PluginsPath</a>), which typically is located in <tt>QTDIR/plugins</tt> (where <tt>QTDIR</tt> is the directory where Qt is installed). If you want Qt to look in additional places you can add as many paths as you need with calls to <a href="qcoreapplication.html#addLibraryPath">QCoreApplication::addLibraryPath</a>(). And if you want to set your own path or paths you can use <a href="qcoreapplication.html#setLibraryPaths">QCoreApplication::setLibraryPaths</a>().</p>
 
55
<p>Suppose that you have a new style class called <tt>MyStyle</tt> that you want to make available as a plugin. The required code is straightforward:</p>
 
56
<pre>&nbsp;   class MyStylePlugin : public QStylePlugin
 
57
    {
 
58
    public:
 
59
        QStringList keys() const {
 
60
            return QStringList() &lt;&lt; &quot;mystyle&quot;;
 
61
        }
 
62
 
 
63
        QStyle *create(const QString &amp;key) {
 
64
            if (key == &quot;mystyle&quot;)
 
65
                return new MyStyle;
 
66
            return 0;
 
67
        }
 
68
    };
 
69
 
 
70
    Q_EXPORT_PLUGIN(MyStylePlugin)</pre>
 
71
<p>(Note that <a href="qstyleplugin.html">QStylePlugin</a> is case-insensitive, and the lower-case version of the key is used; most other plugins are case sensitive.)</p>
 
72
<p>For database drivers, image formats, text codecs, and most other plugin types, no explicit object creation is required. Qt will find and create them as required. Styles are an exception, since you might want to set a style explicitly in code. To apply a style, use code like this:</p>
 
73
<pre>&nbsp;   QApplication::setStyle(QStyleFactory::create(&quot;MyStyle&quot;));</pre>
 
74
<p>Some plugin classes require additional functions to be implemented. See the class documentation for details of the virtual functions that must be reimplemented for each type of plugin.</p>
 
75
<p>Qt applications automatically know which plugins are available, because plugins are stored in the standard plugin subdirectories. Because of this applications don't require any code to find and load plugins, since Qt handles them automatically.</p>
 
76
<p>The default directory for plugins is <tt>QTDIR/plugins</tt> (where <tt>QTDIR</tt> is the directory where Qt is installed), with each type of plugin in a subdirectory for that type, e.g. <tt>styles</tt>. If you want your applications to use plugins and you don't want to use the standard plugins path, have your installation process determine the path you want to use for the plugins, and save the path, e.g. using <a href="qsettings.html">QSettings</a>, for the application to read when it runs. The application can then call <a href="qcoreapplication.html#addLibraryPath">QCoreApplication::addLibraryPath</a>() with this path and your plugins will be available to the application. Note that the final part of the path (e.g., <tt>styles</tt>) cannot be changed.</p>
 
77
<p>The normal way to include a plugin with an application is either to compile it in with the application or to compile it into a dynamic library and use it like any other library. If you want the plugin to be loadable then one approach is to create a subdirectory under the application and place the plugin in that directory.</p>
 
78
<a name="the-lower-level-api-extending-qt-applications"></a>
 
79
<h2>The Lower-Level API: Extending Qt Applications</h2>
 
80
<p>Not only Qt itself but also Qt application can be extended through plugins. This requires the application to detect and load plugins using <a href="qpluginloader.html">QPluginLoader</a>. In that context, plugins may provide arbitrary functionality and are not limited to database drivers, image formats, text codecs, styles, and the other types of plugin that extend Qt's functionality.</p>
 
81
<p>Making an application extensible through plugins involves the following steps:</p>
 
82
<ol type="1">
 
83
<li>Define a set of interfaces (classes with only pure virtual functions) used to talk to the plugins.</li>
 
84
<li>Use the <a href="qpluginloader.html#Q_DECLARE_INTERFACE">Q_DECLARE_INTERFACE</a>() macro to tell Qt's <a href="metaobjects.html">meta-object system</a> about the interface.</li>
 
85
<li>Use <a href="qpluginloader.html">QPluginLoader</a> in the application to load the plugins.</li>
 
86
<li>Use <a href="qobject.html#qobject_cast">qobject_cast</a>() to test whether a plugin implements a given interface.</li>
 
87
</ol>
 
88
<p>Writing a plugin involves these steps:</p>
 
89
<ol type="1">
 
90
<li>Declare a plugin class that inherits from <a href="qobject.html">QObject</a> and from the interfaces that the plugin wants to provide.</li>
 
91
<li>Use the <a href="qobject.html#Q_INTERFACES">Q_INTERFACES</a>() macro to tell Qt's <a href="metaobjects.html">meta-object system</a> about the interfaces.</li>
 
92
<li>Export the plugin using the <a href="qpluginloader.html#Q_EXPORT_PLUGIN">Q_EXPORT_PLUGIN</a>() macro.</li>
 
93
<li>Build the plugin using an adequate <tt>.pro</tt> file.</li>
 
94
</ol>
 
95
<p>For example, here's the definition of an interface class:</p>
 
96
<pre>&nbsp;   class FilterInterface
 
97
    {
 
98
    public:
 
99
        virtual ~FilterInterface() {}
 
100
 
 
101
        virtual QStringList filters() const = 0;
 
102
        virtual QImage filterImage(const QString &amp;filter, const QImage &amp;image,
 
103
                                   QWidget *parent) = 0;
 
104
    };
 
105
 
 
106
    Q_DECLARE_INTERFACE(FilterInterface,
 
107
                        &quot;com.trolltech.PlugAndPaint.FilterInterface/1.0&quot;)</pre>
 
108
<p>Here's the definition of a plugin class that implements that interface:</p>
 
109
<pre>&nbsp;   #include &lt;QObject&gt;
 
110
    #include &lt;QStringList&gt;
 
111
    #include &lt;QImage&gt;
 
112
 
 
113
    #include &lt;plugandpaint/interfaces.h&gt;
 
114
 
 
115
    class ExtraFiltersPlugin : public QObject, public FilterInterface
 
116
    {
 
117
        Q_OBJECT
 
118
        Q_INTERFACES(FilterInterface)
 
119
 
 
120
    public:
 
121
        QStringList filters() const;
 
122
        QImage filterImage(const QString &amp;filter, const QImage &amp;image,
 
123
                           QWidget *parent);
 
124
    };</pre>
 
125
<p>The <a href="tools-plugandpaint.html">Plug &amp; Paint</a> example documentation explains this process in detail. See also <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</a> for information about issues that are specific to <i>Qt Designer</i>.</p>
 
126
<a name="loading-and-verifying-plugins"></a>
 
127
<h2>Loading and Verifying Plugins</h2>
 
128
<p>When loading plugins, the Qt library does some sanity checking to determine whether or not the plugin can be loaded and used. This provides the ability to have multiple versions and configurations of the Qt library installed side by side.</p>
 
129
<ul>
 
130
<li>Plugins linked with a Qt library that has a higher major and/or minor version number will not be loaded by a library with a lower major and/or minor version number.<p><i>Rationale:</i> A plugin linked against a newer Qt library may use new features that are not available in older versions. Trolltech has a policy of adding new features and APIs only between minor releases, which is why this test only looks at the major and minor version numbers, and not at the bug-fix version number.</p>
 
131
</li>
 
132
<li>The Qt library and all plugins are built using a <i>build key</i>. The build key in the Qt library is examined against the build key in the plugin, and if they match, the plugin is loaded. If the build keys do not match, then the Qt library refuses to load the plugin.<p><i>Rationale:</i> See the Rationale for the build key below.</p>
 
133
</li>
 
134
</ul>
 
135
<p>When building plugins to extend an application, it is important to ensure that the plugin is configured in the same way as the application. This means that if the application was built in release mode, plugins should be built in release mode, too.</p>
 
136
<p>If you configure Qt to be built in both debug and release modes, but only build applications in release mode, you need to ensure that your plugins are also built in release mode. By default, if a debug build of Qt is available, plugins will <i>only</i> be built in debug mode. To force the plugins to be built in release mode, add the following line to the plugin's project file:</p>
 
137
<pre>&nbsp;   CONFIG += release</pre>
 
138
<p>This will ensure that the plugin is compatible with the version of the library used in the application.</p>
 
139
<a name="the-build-key"></a>
 
140
<h2>The Build Key</h2>
 
141
<p>When loading plugins, Qt checks the build key of each plugin against its own configuration to ensure that only compatible plugins are loaded; any plugins that are configured differently are not loaded.</p>
 
142
<p>The build key contains the following information:</p>
 
143
<ul>
 
144
<li>Architecture, operating system and compiler.<p><i>Rationale:</i> In cases where different versions of the same compiler do not produce binary compatible code, the version of the compiler is also present in the build key.</p>
 
145
</li>
 
146
<li>Configuration of the Qt library. The configuration is a list of the missing features that affect the available API in the library.<p><i>Rationale:</i> Two different configurations of the same version of the Qt library are not binary compatible. The Qt library that loads the plugin uses the list of (missing) features to determine if the plugin is binary compatible.</p>
 
147
<p><i>Note:</i> There are cases where a plugin can use features that are available in two different configurations. However, the developer writing plugins would need to know which features are in use, both in their plugin and internally by the utility classes in Qt. The Qt library would require complex feature and dependency queries and verification when loading plugins. Requiring this would place an unnecessary burden on the developer, and increase the overhead of loading a plugin. To reduce both development time and application runtime costs, a simple string comparision of the build keys is used.</p>
 
148
</li>
 
149
<li>Optionally, an extra string may be specified on the configure script command line.<p><i>Rationale:</i> When distributing binaries of the Qt library with an application, this provides a way for developers to write plugins that can only be loaded by the library with which the plugins were linked.</p>
 
150
</li>
 
151
</ul>
 
152
<p>See also <a href="qpluginloader.html">QPluginLoader</a> and <a href="qlibrary.html">QLibrary</a>.</p>
 
153
<p /><address><hr /><div align="center">
 
154
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
155
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
156
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
157
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
158
</tr></table></div></address></body>
 
159
</html>