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 5: Line 5:
The example code below shows how you can extend 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 /><code><br />#include <QtGui><br />#include <QtDeclarative>
main.cpp
<code>
#include <QtGui>
#include <QtDeclarative>


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 /> {}
namespace MyNamespace {
class Person : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
public:
Person()
{}


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


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


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


#include "main.moc"
#include "main.moc"
Line 19: Line 48:
using namespace MyNamespace;
using namespace MyNamespace;


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


main.qml
main.qml


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


Text {<br /> text: myPerson.name
Text {
text: myPerson.name


Person {<br /> id: myPerson;<br /> name: "Sigurd"<br /> shoeSize: 24
Person {
id: myPerson;
name: "Sigurd"
shoeSize: 24


}<br />}<br /></code>
}
}
</code>

Revision as of 12:08, 25 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

#include <QtGui>
#include <QtDeclarative>

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

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

int shoeSize() const
 {
 return m_size;
 }
 void setShoeSize(int s)
 {
 m_size = s;
 }

private:
 QString m_name;
 int m_size;
 };
}

#include "main.moc"

using namespace MyNamespace;

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

main.qml

import QtQuick 1.0
import People 1.0

Text {
 text: myPerson.name

Person {
 id: myPerson;
 name: "Sigurd"
 shoeSize: 24

}
}