Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 226

qtdeclarative5-doc-5.12.6-1.mga7.noarch.rpm

<?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.12.6</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.12</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"><a href="qtqml-index.html">Qt 5.12.6 Reference Documentation</a></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="#timeexample-qml-extension-plugin">TimeExample QML extension plugin</a></li>
<li class="level1"><a href="#project-settings-for-the-plugin">Project settings for the plugin</a></li>
<li class="level1"><a href="#plugin-definition-in-the-qmldir">Plugin definition in the qmldir</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 Q_PLUGIN_METADATA() 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="timeexample-qml-extension-plugin"></a>
<h2 id="timeexample-qml-extension-plugin">TimeExample QML extension plugin</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">QObject</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 Q_PLUGIN_METADATA() 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) override
      {
          Q_ASSERT(uri <span class="operator">=</span><span class="operator">=</span> QLatin1String(<span class="string">&quot;TimeExample&quot;</span>));
          qmlRegisterType<span class="operator">&lt;</span>TimeModel<span class="operator">&gt;</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">&quot;Time&quot;</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 Q_ASSERT() 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>
<a name="project-settings-for-the-plugin"></a>
<h2 id="project-settings-for-the-plugin">Project settings for the plugin</h2>
<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>
<a name="plugin-definition-in-the-qmldir"></a>
<h2 id="plugin-definition-in-the-qmldir">Plugin definition in the qmldir</h2>
<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 // import types from the plugin

  Clock { // this class is defined in QML (imports/TimeExample/Clock.qml)

      Time { // this class is defined in C++ (plugin.cpp)
          id: time
      }

      hours: time.hour
      minutes: time.minute

  }

</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">&copy;</acronym> 2019 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>