<?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.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 >Integrating QML and C++</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="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 QObject. 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">QObject</span> { Q_OBJECT Q_PROPERTY(<span class="type">QString</span> userName READ userName WRITE setUserName NOTIFY userNameChanged) <span class="keyword">public</span>: <span class="keyword">explicit</span> BackEnd(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr); <span class="type">QString</span> userName(); <span class="type">void</span> setUserName(<span class="keyword">const</span> <span class="type">QString</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">QString</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">QObject</span> <span class="operator">*</span>parent) : <span class="type">QObject</span>(parent) { } <span class="type">QString</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">QString</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">QGuiApplication</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">QUrl</span>(<span class="type">QStringLiteral</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 ApplicationWindow { id: root width: 300 height: 480 visible: true BackEnd { id: backend } TextField { text: backend.userName placeholderText: qsTr("User name") anchors.centerIn: parent onTextChanged: backend.userName = text } } </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> 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>