QT Mobile Application Development (vi): QML and C + + interaction

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p style="text-align:center"><p style="text-align:center"><span style="font-family:SimHei; font-size:32px">QT Mobile Application Development (vi): QML and C + + interaction</span></p></p><p><p></p></p><p><p>The previous article described a possible way to achieve scene switching in QT quick, scene switching is a game and other applications in the need to face the technical difficulties, so the scene switch and there is no way, according to their own habits of design can Be.</p></p><p><p>This article mainly introduces how to use QML and C + + to interact, the difficulty is slightly larger, suitable for experienced QT developers to exchange Learning.</p></p><p><p>QT 5 absorbs the strengths of the QT 4 declarative module, changes the underlying, creates a new QPA layer, isolates the different operating system APIs and the upper Qt Code. At the same time Qml/qtquick can also be executed on different platforms smoothly. In addition, since the QT program is considered to have access to different library functions, Qt opens the interface to let the QML layer and C + + code Interact.</p></p><p><p>There have been more articles about QML and C + + interaction, This article is only as a deliberate supplement, many other related knowledge can query Qt Help document or message to Me.</p></p><p><p>The sample in this article was successfully compiled and executed in QT 5.3.1.</p></p><p><p></p></p><p><p><span style="color:#9966FF; background:#9966FF">original article, against undeclared references. Original blog address:</span><span style="color:#9966FF; background:#9966FF">http://blog.csdn.net/gamesdev/article/details/37359873</span></p></p><p><p>first, A simpler approach is to register the context Property. Ask QML to access the C + + Variables.</p></p><p><p>The code is as Follows:</p></p><pre><pre><span style="color:navy"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_1_2967261" name="code" class="cpp"><pre code_snippet_id="419185" snippet_file_name="blog_20140706_1_2967261" name="code" class="cpp">#include <QApplication> #include <QQmlApplicationEngine> #include <qqmlcontext>int main (int argc, Char *argv[]) { qapplication app (argc, argv); Qqmlapplicationengine engine; Engine.rootcontext ()->setcontextproperty ( "greeting", qobject::tr ("Hello QML from C + +")); Engine.load (qurl (qstringliteral ("qrc:///main.qml")); return app.exec ();}</pre></pre><pre><pre></pre></pre><p><p>Then simply call the "greeting" variable name in QML to get a good interview.</p></p><pre><pre><span style="color:olive"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_2_7536830" name="code" class="css"><pre code_snippet_id="419185" snippet_file_name="blog_20140706_2_7536830" name="code" class="css">Import QtQuick 2.2import qtquick.controls 1.1applicationwindow{ visible:true width:640 height:480 Title:qstr ("test QML in C + + Interaction") menubar:menubar { menu { title:qstr ("file") MenuItem { text:qstr ("exit") onTriggered:Qt.quit (); }}} The Text { text:qstr ("this example is used to test QML and C + + Interactions") anchors.right:parent.right anchors.bottom: Parent.bottom } Text { text:greeting anchors.centerIn:parent }}</pre></pre><br><pre><pre></pre></pre><p><p>The following are examples of demo programs:</p></p><p><p></p></p><p><p>An important part of this example is the Qqmlcontext instance Pointer. It is obtained through Qqmlapplicationengine::rootcontext () and can also be passed qqmlapplicationengine:: contextforobject (constqobject * Object) To Obtain.</p></p><p><p>At the time of Qqmlobject Creation. Will instantiate a Qqmlcontext. Used to support the context properties provided for the execution Environment.</p></p><p><p>Using context properties allows QML to access C + + data, So how do you use QML to visit C + + functions? Here we register the QML class or Singleton in C + + to let QML get access to C + + Functions. Let's start by introducing how to QML the C + + class into Qml. first, you need to define a C + + class to inherit from qobject, and then write:</p></p><pre><pre><span style="color:navy"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_6_2597551" name="code" class="cpp"><pre code_snippet_id="419185" snippet_file_name="blog_20140706_6_2597551" name="code" class="cpp">#ifndef cplusplusclass_h#define cplusplusclass_h#include <qobject>class cplusplusclass:public QObject{ Q_ OBJECT q_property (int rating READ Rating) public: explicit cplusplusclass (qobject* pparent = q_nullptr): Q Object (pparent) { m_rating = 5; } q_invokable void method (void) { qdebug ("[c++]%s is called.", __function__); } int rating (void) {return m_rating;} private: int m_rating;}; #endif//cplusplusclass_h</pre></pre><pre><pre><span style="color:green"></span></pre></pre><p><p>Then the main.cpp need to call the Qmlregistertype () template function to register the C + + class into qml, a typical use method such as the Following:</p></p><pre><pre><span style="color:navy"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_4_7122909" name="code" class="cpp"><pre code_snippet_id="419185" snippet_file_name="blog_20140706_4_7122909" name="code" class="cpp">#include <QApplication> #include <QQmlApplicationEngine> #include <QtQml> #include " CPlusPlusClass.h "int main (int argc, Char *argv[]) { qapplication app (argc, argv); First note the class qmlregistertype<cplusplusclass> ( "cplusplus.test",/ /uniform Resource Identifier 1,// major version number 0, //minor version number "cplusplustype"); QML class name qqmlapplicationengine engine; Engine.load (qurl (qstringliteral ("qrc:///main.qml")); return app.exec ();}</pre></pre><pre><pre></pre></pre><p><p>finally, You can access the properties and methods of C + + classes smoothly in Qml:</p></p><pre><pre><span style="color:olive"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_5_6359541" name="code" class="cpp"><pre code_snippet_id="419185" snippet_file_name="blog_20140706_5_6359541" name="code" class="cpp">Import QtQuick 2.2import qtquick.controls 1.1import cplusplus.test 1.0applicationwindow{ visible:true width: 640 height:480 title:qstr ("test qml in C + + Interaction") menubar:menubar { menu { title:qstr ("file" ) MenuItem { text:qstr ("exit") onTriggered:Qt.quit (); }}} The Text { text:qstr ("this example is used to test QML and C + + Interactions") anchors.right:parent.right anchors.bottom: Parent.bottom } cplusplustype { id:thetype } mousearea { anchors.fill: Parent onclicked: { console.log ("[qml] Rating is:" + thetype.rating); Thetype.method ();}}} </pre></pre><pre><pre></pre></pre><p><p>Click on the window, console execution results such as the following:</p></p><p align="left"><p align="left"><span style="color:#AA0000">Qml: [qml] Ratingis:5</span></p></p><p><p><span style="color:#AA0000">[c++]method Iscalled.</span></p></p><p><p>Suppose you don't want to create multiple qobject in QML and C + + environments or to make it easier to access C + + METHODS. Then it is possible to consider a single class, a single class and a register the common classes are almost identical, but there are some notable differences, first of all to establish such a class that inherits from Qobject. The code is as Follows:</p></p><pre><pre><span style="color:navy"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_6_2597551" name="code" class="cpp"><pre code_snippet_id="419185" snippet_file_name="blog_20140706_6_2597551" name="code" class="cpp">#ifndef cplusplusclass_h#define cplusplusclass_h#include <qobject>class cplusplusclass:public QObject{ Q_ OBJECT q_property (int rating READ Rating) public: explicit cplusplusclass (qobject* pparent = q_nullptr): Q Object (pparent) { m_rating = 5; } q_invokable void method (void) { qdebug ("[c++]%s is called.", __function__); } int rating (void) {return m_rating;} private: int m_rating;}; #endif//cplusplusclass_h</pre></pre><pre><pre><span style="color:green"></span></pre></pre><p><p>Then the key is in the Main.cpp. In addition to calling the Qmlregistersingletontype () template Function. You also need to write a static global register Function. The code is as Follows:</p></p><pre><pre><span style="color:navy"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_7_9325927" name="code" class="cpp">#include <QApplication> #include <QQmlApplicationEngine> #include <QtQml> #include " CPlusPlusSingleton.h "//register Single example function static qobject* cplusplussingletonregisterfunc (qqmlengine* pqmlengine, Qjseng ine* pjsengine) {q_unused (pqmlengine); Q_unused (pjsengine); cplusplussingleton* Psingleton = new cplusplussingleton; Return psingleton;} int main (int argc, char *argv[]) {qapplication app (argc, argv); first, Please register the Singleton qmlregistersingletontype<cplusplussingleton> ("cplusplus.test",// A resource identifier 1,//major version number 0,//minor version This number "cplusplussingleton",//single case name cplusplussingletonregisterfunc); Function name Qqmlapplicationengine engine; Engine.load (qurl (qstringliteral ("qrc:///main.qml")); return app.exec ();}</pre><pre><pre></pre></pre><p><p>That's it. The C + + section is Complete.</p></p><p><p>The next step in QML is very easy:</p></p><pre><pre><span style="color:olive"></span></pre></pre><pre code_snippet_id="419185" snippet_file_name="blog_20140706_8_1594203" name="code" class="css"><pre code_snippet_id="419185" snippet_file_name="blog_20140706_8_1594203" name="code" class="css">Import QtQuick 2.2import qtquick.controls 1.1import cplusplus.test 1.0applicationwindow{ visible:true width: 640 height:480 title:qstr ("test qml in C + + Interaction") menubar:menubar { menu { title:qstr ("file" ) MenuItem { text:qstr ("exit") onTriggered:Qt.quit (); }}} The Text { text:qstr ("this example is used to test QML and C + + Interactions") anchors.right:parent.right anchors.bottom: Parent.bottom } mousearea { anchors.fill:parent onclicked: { console.log ("[ qml] Rating is: "+ cplusplussingleton.rating); Cplusplussingleton.method ();}}} </pre></pre><pre><pre></pre></pre><p><p>Click the WINDOW. Console results such as the following:</p></p><p align="left"><p align="left"><span style="color:#AA0000">Qml: [qml] Ratingis:5</span></p></p><p><p><span style="color:#AA0000">[c++]method Iscalled.</span></p></p><p><p>It is possible to obtain the corresponding characteristics based on the need to choose whether to register the QML class and the C + + single case in C + +.</p></p><p><p>In my first independent game "take medicine", in order to successfully access the advertising sdk, you need to write C + + code to ensure that QML can visit the C + + function, advertising display effect such as the following:</p></p><p><p></p></p><p><p></p></p><p><p>articleid=37359873 "style=" Color:rgb (51,102,153); text-decoration:none; font-family:arial; font-size:14px; LINE-HEIGHT:26PX "> This article participated in CSDN Bowen contest <span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">. Please let us support me and choose Me. </span><br></p></p><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article blog original Article. blogs, without consent, may not be reproduced.</p></p><p><p></p></p> <p><p>QT Mobile Application Development (vi): QML and C + + interaction</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.