<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QDeclarativeExtensionPlugin Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QDeclarativeExtensionPlugin Class Reference<br /><sup><sup>[<a href="qtdeclarative.html">QtDeclarative</a> module]</sup></sup></h1><p>The QDeclarativeExtensionPlugin class provides an abstract base for custom QML extension plugins. <a href="#details">More...</a></p> <p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qdeclarativeextensionplugin.html#QDeclarativeExtensionPlugin">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None)</li><li><div class="fn" /><b><a href="qdeclarativeextensionplugin.html#initializeEngine">initializeEngine</a></b> (<i>self</i>, QDeclarativeEngine <i>engine</i>, str <i>uri</i>)</li><li><div class="fn" /><b><a href="qdeclarativeextensionplugin.html#registerTypes">registerTypes</a></b> (<i>self</i>, str <i>uri</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QDeclarativeExtensionPlugin class provides an abstract base for custom QML extension plugins.</p> <p>QDeclarativeExtensionPlugin is a plugin interface that makes it possible to create QML extensions that can be loaded dynamically into QML applications. These extensions allow custom QML types to be made available to the QML engine.</p> <p>To write a QML extension plugin:</p> <ul> <li>Subclass QDeclarativeExtensionPlugin, implement <a href="qdeclarativeextensionplugin.html#registerTypes">registerTypes</a>() method to register types using <a href="qdeclarativeengine.html#qmlRegisterType">qmlRegisterType</a>(), and export the class using the <a href="qtplugin.html#Q_EXPORT_PLUGIN2">Q_EXPORT_PLUGIN2</a>() macro</li> <li>Write an appropriate project file for the plugin</li> <li>Create a <a href="qdeclarativemodules.html#writing-a-qmldir-file">qmldir file</a> to describe the plugin</li> </ul> <p>QML extension plugins can be used to provide either application-specific or library-like plugins. Library plugins should limit themselves to registering types, as any manipulation of the engine's root context may cause conflicts or other issues in the library user's code.</p> <a id="an-example" name="an-example" /> <h3>An example</h3> <p>Suppose there is a new <tt>TimeModel</tt> C++ class that should be made available as a new QML element. It provides the current time through <tt>hour</tt> and <tt>minute</tt> properties, like this:</p> <pre class="cpp"> <span class="keyword">class</span> TimeModel : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span> { Q_OBJECT Q_PROPERTY(<span class="type">int</span> hour READ hour NOTIFY timeChanged) Q_PROPERTY(<span class="type">int</span> minute READ minute NOTIFY timeChanged) ... </pre> <p>To make this class available as a QML type, create a plugin that registers this type with a specific <a href="qdeclarativemodules.html#qml-modules">module</a> using <a href="qdeclarativeengine.html#qmlRegisterType">qmlRegisterType</a>(). For this example the plugin module will be named <tt>com.nokia.TimeExample</tt> (as defined in the project file further below).</p> <pre class="cpp"> <span class="keyword">class</span> <span class="type">QExampleQmlPlugin</span> : <span class="keyword">public</span> <span class="type">QDeclarativeExtensionPlugin</span> { Q_OBJECT <span class="keyword">public</span>: <span class="type">void</span> registerTypes(<span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>uri) { Q_ASSERT(uri <span class="operator">=</span><span class="operator">=</span> QLatin1String(<span class="string">"com.nokia.TimeExample"</span>)); qmlRegisterType<span class="operator"><</span>TimeModel<span class="operator">></span>(uri<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">"Time"</span>); } }; <a href="qtplugin.html#Q_EXPORT_PLUGIN2">Q_EXPORT_PLUGIN2</a>(qmlqtimeexampleplugin<span class="operator">,</span> <span class="type">QExampleQmlPlugin</span>); </pre> <p>This registers the <tt>TimeModel</tt> class with the 1.0 version of this plugin library, as a QML type called <tt>Time</tt>. The Q_ASSERT statement ensures the module is imported correctly by any QML components that use this plugin.</p> <p>The project file defines the project as a plugin library and specifies it should be built into the <tt>com/nokia/TimeExample</tt> directory:</p> <pre class="cpp"> TEMPLATE <span class="operator">=</span> lib CONFIG <span class="operator">+</span><span class="operator">=</span> qt plugin QT <span class="operator">+</span><span class="operator">=</span> declarative DESTDIR <span class="operator">=</span> com<span class="operator">/</span>nokia<span class="operator">/</span>TimeExample TARGET <span class="operator">=</span> qmlqtimeexampleplugin <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> </pre> <p>Finally, a <a href="qdeclarativemodules.html#writing-a-qmldir-file">qmldir file</a> is required in the <tt>com/nokia/TimeExample</tt> directory that describes the plugin. This directory includes a <tt>Clock.qml</tt> file that should be bundled with the plugin, so it needs to be specified in the <tt>qmldir</tt> file:</p> <pre class="cpp"> Clock <span class="number">1.0</span> Clock<span class="operator">.</span>qml plugin qmlqtimeexampleplugin </pre> <p>Once the project is built and installed, the new <tt>Time</tt> element can be used by any QML component that imports the <tt>com.nokia.TimeExample</tt> module:</p> <pre class="qml"> import com.nokia.TimeExample 1.0 <span class="comment">// import types from the plugin</span> <span class="type">Clock</span> { <span class="comment">// this class is defined in QML (com/nokia/TimeExample/Clock.qml)</span> <span class="type">Time</span> { <span class="comment">// this class is defined in C++ (plugin.cpp)</span> <span class="name">id</span>: <span class="name">time</span> } <span class="name">hours</span>: <span class="name">time</span>.<span class="name">hour</span> <span class="name">minutes</span>: <span class="name">time</span>.<span class="name">minute</span> } </pre> <p>The full source code is available in the <a href="declarative-cppextensions-plugins.html">plugins example</a>.</p> <p>The <a href="qml-extending-tutorial-index.html">Tutorial: Writing QML extensions with C++</a> also contains a chapter on creating QML plugins.</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QDeclarativeExtensionPlugin" />QDeclarativeExtensionPlugin.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a QML extension plugin with the given <i>parent</i>.</p> <p>Note that this constructor is invoked automatically by the <a href="qtplugin.html#Q_EXPORT_PLUGIN2">Q_EXPORT_PLUGIN2</a>() macro, so there is no need for calling it explicitly.</p> <h3 class="fn"><a name="initializeEngine" />QDeclarativeExtensionPlugin.initializeEngine (<i>self</i>, <a href="qdeclarativeengine.html">QDeclarativeEngine</a> <i>engine</i>, str <i>uri</i>)</h3><p>Initializes the extension from the <i>uri</i> using the <i>engine</i>. Here an application plugin might, for example, expose some data or objects to QML, as context properties on the engine's root context.</p> <h3 class="fn"><a name="registerTypes" />QDeclarativeExtensionPlugin.registerTypes (<i>self</i>, str <i>uri</i>)</h3><p>This method is abstract and should be reimplemented in any sub-class.</p><p>Registers the QML types in the given <i>uri</i>. Subclasses should implement this to call <a href="qdeclarativeengine.html#qmlRegisterType">qmlRegisterType</a>() for all types which are provided by the extension plugin.</p> <p>The <i>uri</i> is an identifier for the plugin generated by the QML engine based on the name and path of the extension's plugin library.</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.10.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>