/usr/share/qt5/doc/qtqml/qtqml-modules-cppplugins.html is in qtdeclarative5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- cppplugins.qdoc -->
<title>Creating C++ Plugins for QML | Qt QML 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.9</td><td ><a href="qtqml-index.html">Qt QML</a></td><td >Creating C++ Plugins for QML</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#creating-a-plugin">Creating a Plugin</a></li>
<li class="level1"><a href="#plugin-example">Plugin Example</a></li>
<li class="level1"><a href="#reference">Reference</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Creating C++ Plugins for QML</h1>
<span class="subtitle"></span>
<!-- $$$qtqml-modules-cppplugins.html-description -->
<div class="descr"> <a name="details"></a>
<a name="creating-a-plugin"></a>
<h2 id="creating-a-plugin">Creating a Plugin</h2>
<p>The <a href="qqmlengine.html">QML engine</a> loads C++ plugins for QML. Such plugins are usually provided in a QML extension module, and can provide types for use by clients in QML documents which import the module. A module requires at least one type registered in order to be considered valid.</p>
<p><a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a> 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>
<ol class="1" type="1"><li>Subclass <a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a><ul>
<li>Use the <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>() macro to register the plugin with the Qt meta object system</li>
<li>Override the <a href="qqmlextensionplugin.html#registerTypes">registerTypes()</a> method and call <a href="qqmlengine.html#qmlRegisterType">qmlRegisterType</a>() to register the types to be exported by the plugin</li>
</ul>
</li>
<li>Write a project file for the plugin</li>
<li>Create a <a href="qtqml-modules-qmldir.html">qmldir file</a> to describe the plugin</li>
</ol>
<p>QML extension plugins are for 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 name="plugin-example"></a>
<h2 id="plugin-example">Plugin Example</h2>
<p>Suppose there is a new <code>TimeModel</code> C++ class that should be made available as a new QML type. It provides the current time through <code>hour</code> and <code>minute</code> properties.</p>
<pre class="cpp">
<span class="keyword">class</span> TimeModel : <span class="keyword">public</span> <span class="type"><a href="../qtcore/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 type available, we create a plugin class named <code>QExampleQmlPlugin</code> which is a subclass of <a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a>. It overrides the <a href="qqmlextensionplugin.html#registerTypes">registerTypes()</a> method in order to register the <code>TimeModel</code> type using <a href="qqmlengine.html#qmlRegisterType">qmlRegisterType</a>(). It also uses the <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>() macro in the class definition to register the plugin with the Qt meta object system using a unique identifier for the plugin.</p>
<pre class="cpp">
<span class="keyword">class</span> <span class="type">QExampleQmlPlugin</span> : <span class="keyword">public</span> <span class="type"><a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a></span>
{
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
<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">"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>);
}
};
</pre>
<p>This registers the <code>TimeModel</code> class with version <code>1.0</code> of this plugin library, as a QML type called <code>Time</code>. The <a href="../qtcore/qtglobal.html#Q_ASSERT">Q_ASSERT</a>() macro can ensure the type namespace is imported correctly by any QML components that use this plugin. The <a href="qtqml-cppintegration-definetypes.html">Defining QML Types from C++</a> article has more information about registering C++ types into the runtime.</p>
<p>Additionally, the project file (<code>.pro</code>) defines the project as a plugin library, specifies it should be built into the <code>imports/TimeExample</code> directory, and registers the plugin target name and various other details:</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> qml
DESTDIR <span class="operator">=</span> imports<span class="operator">/</span>TimeExample
TARGET <span class="operator">=</span> qmlqtimeexampleplugin
SOURCES <span class="operator">+</span><span class="operator">=</span> qexampleqmlplugin<span class="operator">.</span>cpp
</pre>
<p>Finally, a <a href="qtqml-modules-qmldir.html">qmldir file</a> is required in the <code>imports/TimeExample</code> directory to describe the plugin and the types that it exports. The plugin includes a <code>Clock.qml</code> file along with the <code>qmlqtimeexampleplugin</code> that is built by the project (as shown above in the <code>.pro</code> file) so both of these need to be specified in the <code>qmldir</code> file:</p>
<pre class="cpp">
module TimeExample
Clock <span class="number">1.0</span> Clock<span class="operator">.</span>qml
plugin qmlqtimeexampleplugin
</pre>
<p>To make things easier for this example, the TimeExample source directory is in <code>imports/TimeExample</code>, and we build in-source. However, the structure of the source directory is not so important, as the <code>qmldir</code> file can specify paths to installed QML files.</p>
<p>What is important is the name of the directory that the qmldir is installed into. When the user imports our module, the QML engine uses the <a href="qtqml-modules-qmldir.html#contents-of-a-module-definition-qmldir-file">module identifier</a> (<code>TimeExample</code>) to find the plugin, and so the directory in which it is installed must match the module identifier.</p>
<p>Once the project is built and installed, the new <code>Time</code> component is accessible by any QML component that imports the <code>TimeExample</code> module</p>
<pre class="qml">
import 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 (imports/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="qtqml-qmlextensionplugins-example.html">plugins example</a>.</p>
<a name="reference"></a>
<h2 id="reference">Reference</h2>
<ul>
<li><a href="qtqml-tutorials-extending-qml-example.html">Writing QML Extensions with C++</a> - contains a chapter on creating QML plugins.</li>
<li><a href="qtqml-cppintegration-definetypes.html">Defining QML Types from C++</a> - information about registering C++ types into the runtime.</li>
<li>How to Create Qt Plugins - information about Qt plugins</li>
</ul>
</div>
<!-- @@@qtqml-modules-cppplugins.html -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|