Extending QML with a C++ class in a namespace: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Extending <span class="caps">QML</span> with a C++ class in a custom namespace=
[[Category:snippets]]


The example code below shows how you can extend <span class="caps">QML</span> with a C++ class in a custom namespace.
= Extending QML with a C++ class in a custom namespace =


main.cpp<br />
The example code below shows how you can extend QML with a C++ class in a custom namespace.
 
main.cpp<br /><code><br />#include &lt;QtGui&amp;gt;<br />#include &lt;QtDeclarative&amp;gt;
 
namespace MyNamespace {<br /> class Person : public QObject<br /> {<br /> Q_OBJECT<br /> Q_PROPERTY(QString name READ name WRITE setName)<br /> Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)<br /> public:<br /> Person()<br /> {}
 
QString name() const<br /> {<br /> return m_name;<br /> }<br /> void setName(const QString &amp; n)<br /> {<br /> m_name = n;<br /> }
 
int shoeSize() const<br /> {<br /> return m_size;<br /> }<br /> void setShoeSize(int s)<br /> {<br /> m_size = s;<br /> }
 
private:<br /> QString m_name;<br /> int m_size;<br /> };<br />}
 
#include &quot;main.moc&amp;quot;
 
using namespace MyNamespace;
 
int main(int argc, char** argv)<br />{<br /> QApplication app(argc, argv);<br /> qmlRegisterType&amp;lt;Person&amp;gt;(&quot;People&amp;quot;, 1, 0, &quot;Person&amp;quot;);<br /> MyNamespace::Person myObj;<br /> QDeclarativeView view;<br /> view.rootContext()-&gt;setContextProperty(&quot;rootItem&amp;quot;, (Person *)&amp;myObj);<br /> view.setSource(QUrl::fromLocalFile&amp;amp;#40;&quot;main.qml&amp;quot;&amp;#41;);<br /> view.resize(200,100);<br /> view.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>


main.qml
main.qml


===Categories:===
<code><br />import QtQuick 1.0<br />import People 1.0
 
Text {<br /> text: myPerson.name
 
Person {<br /> id: myPerson;<br /> name: &quot;Sigurd&amp;quot;<br /> shoeSize: 24


* [[:Category:snippets|snippets]]
}<br />}<br /></code>

Revision as of 06:15, 24 February 2015


Extending QML with a C++ class in a custom namespace

The example code below shows how you can extend QML with a C++ class in a custom namespace.

main.cpp

<br />#include &lt;QtGui&amp;gt;<br />#include &lt;QtDeclarative&amp;gt;

namespace MyNamespace {<br /> class Person : public QObject<br /> {<br /> Q_OBJECT<br /> Q_PROPERTY(QString name READ name WRITE setName)<br /> Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)<br /> public:<br /> Person()<br /> {}

QString name() const<br /> {<br /> return m_name;<br /> }<br /> void setName(const QString &amp; n)<br /> {<br /> m_name = n;<br /> }

int shoeSize() const<br /> {<br /> return m_size;<br /> }<br /> void setShoeSize(int s)<br /> {<br /> m_size = s;<br /> }

private:<br /> QString m_name;<br /> int m_size;<br /> };<br />}

#include &quot;main.moc&amp;quot;

using namespace MyNamespace;

int main(int argc, char** argv)<br />{<br /> QApplication app(argc, argv);<br /> qmlRegisterType&amp;lt;Person&amp;gt;(&quot;People&amp;quot;, 1, 0, &quot;Person&amp;quot;);<br /> MyNamespace::Person myObj;<br /> QDeclarativeView view;<br /> view.rootContext()-&gt;setContextProperty(&quot;rootItem&amp;quot;, (Person *)&amp;myObj);<br /> view.setSource(QUrl::fromLocalFile&amp;amp;#40;&quot;main.qml&amp;quot;&amp;#41;);<br /> view.resize(200,100);<br /> view.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br />

main.qml

<br />import QtQuick 1.0<br />import People 1.0

Text {<br /> text: myPerson.name

Person {<br /> id: myPerson;<br /> name: &quot;Sigurd&amp;quot;<br /> shoeSize: 24

}<br />}<br />