/usr/share/qt5/doc/qtqml/qtqml-cppintegration-topic.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 159 160 161 162 163 164 165 166 167 168 169 170 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- topic.qdoc -->
<title>Integrating QML and C++ | 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 >Integrating QML and C++</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="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Integrating QML and C++</h1>
<span class="subtitle"></span>
<!-- $$$qtqml-cppintegration-topic.html-description -->
<div class="descr"> <a name="details"></a>
<p>QML applications often need to handle more advanced and performance-intensive tasks in C++. The most common and quickest way to do this is to expose the C++ class to the QML runtime, provided the C++ implementation is derived from <a href="../qtcore/qobject.html">QObject</a>. Assuming that you have Qt 5.7 or later installed, the following step-by-step instructions guide you through the process of using the C++ class, BackEnd, in a QML application:</p>
<ol class="1" type="1"><li>Create a new project using the "Qt Quick Application" template in Qt Creator<p><b>Note: </b>Uncheck the <b>With ui.qml file</b> option in the <b>Define Project Details</b> section of <b>New Project Wizard</b>.</p></li>
<li>Add a new C++ class called <code>BackEnd</code> to the project and replace its header file contents with:<pre class="cpp">
<span class="preprocessor">#ifndef BACKEND_H</span>
<span class="preprocessor">#define BACKEND_H</span>
<span class="preprocessor">#include <QObject></span>
<span class="preprocessor">#include <QString></span>
<span class="keyword">class</span> BackEnd : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
Q_PROPERTY(<span class="type"><a href="../qtcore/qstring.html">QString</a></span> userName READ userName WRITE setUserName NOTIFY userNameChanged)
<span class="keyword">public</span>:
<span class="keyword">explicit</span> BackEnd(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> userName();
<span class="type">void</span> setUserName(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>userName);
<span class="keyword">signals</span>:
<span class="type">void</span> userNameChanged();
<span class="keyword">private</span>:
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> m_userName;
};
<span class="preprocessor">#endif // BACKEND_H</span>
</pre>
<p>The <code>Q_PROPERTY</code> macro declares a property that could be accessed from QML.</p>
</li>
<li>Replace its C++ file contents with:<pre class="cpp">
<span class="preprocessor">#include "backend.h"</span>
BackEnd<span class="operator">::</span>BackEnd(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent) :
<span class="type"><a href="../qtcore/qobject.html">QObject</a></span>(parent)
{
}
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> BackEnd<span class="operator">::</span>userName()
{
<span class="keyword">return</span> m_userName;
}
<span class="type">void</span> BackEnd<span class="operator">::</span>setUserName(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>userName)
{
<span class="keyword">if</span> (userName <span class="operator">=</span><span class="operator">=</span> m_userName)
<span class="keyword">return</span>;
m_userName <span class="operator">=</span> userName;
<span class="keyword">emit</span> userNameChanged();
}
</pre>
<p>The <code>setUserName</code> function emits the <code>userNameChanged</code> signal every time <code>m_userName</code> value changes. The signal can be handled from QML using the <code>onUserNameChanged</code> handler.</p>
</li>
<li>Include <code>"backend.h"</code> in <code>main.cpp</code> and register the class as a QML type under a import URL as shown below:<pre class="cpp">
<span class="preprocessor">#include <QGuiApplication></span>
<span class="preprocessor">#include <QQmlApplicationEngine></span>
<span class="preprocessor">#include "backend.h"</span>
<span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
{
<span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span> app(argc<span class="operator">,</span> argv);
qmlRegisterType<span class="operator"><</span>BackEnd<span class="operator">></span>(<span class="string">"io.qt.examples.backend"</span><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">"BackEnd"</span>);
<span class="type"><a href="qqmlapplicationengine.html">QQmlApplicationEngine</a></span> engine;
engine<span class="operator">.</span>load(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"qrc:/main.qml"</span>)));
<span class="keyword">return</span> app<span class="operator">.</span>exec();
}
</pre>
<p>The BackEnd class is registered as a type, which is accessible from QML by importing the URL, "<code>io.qt.examples.backend 1.0</code>".</p>
</li>
<li>Replace the contents of <code>main.qml</code> with the following code:<pre class="qml">
import QtQuick 2.6
import QtQuick.Controls 2.0
import io.qt.examples.backend 1.0
<span class="type">ApplicationWindow</span> {
<span class="name">id</span>: <span class="name">root</span>
<span class="name">width</span>: <span class="number">300</span>
<span class="name">height</span>: <span class="number">480</span>
<span class="name">visible</span>: <span class="number">true</span>
<span class="type">BackEnd</span> {
<span class="name">id</span>: <span class="name">backend</span>
}
<span class="type">TextField</span> {
<span class="name">text</span>: <span class="name">backend</span>.<span class="name">userName</span>
<span class="name">placeholderText</span>: <span class="name">qsTr</span>(<span class="string">"User name"</span>)
<span class="name">anchors</span>.centerIn: <span class="name">parent</span>
<span class="name">onTextChanged</span>: <span class="name">backend</span>.<span class="name">userName</span> <span class="operator">=</span> <span class="name">text</span>
}
}
</pre>
<p>The <code>BackEnd</code> instance lets you access the <code>userName</code> property, which is updated when the TextField's <code>text</code> property changes.</p>
</li>
</ol>
<p>Now the application can be run.</p>
<div class="border"><p class="centerAlign"><img src="images/cppintegration-ex.png" alt="" /></p></div><p class="figCaption">Application running on Ubuntu</p>
<p>Qt offers several methods to integrate C++ with QML, and the method discussed in this tutorial is just one of them. For more details about these methods, refer to <a href="qtqml-cppintegration-overview.html">Overview - QML and C++ Integration</a>.</p>
</div>
<!-- @@@qtqml-cppintegration-topic.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>
|