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

From Qt Wiki
Jump to navigation Jump to search
(Decode HTML entity names)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:Snippets::QML]]
[[Category:snippets]]
 
= 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.
The example code below shows how you can extend QML with a C++ class in a custom namespace.


Line 13: Line 9:


namespace MyNamespace {
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
class Person : public QObject
{
{
return m_name;
    Q_OBJECT
}
    Q_PROPERTY(QString name READ name WRITE setName)
void setName(const QString & n)
    Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
{
public:
m_name = n;
    Person() {}
}
    QString name() const { return m_name; }
    void setName(const QString& n) { m_name = n; }
    int shoeSize() const { return m_size; }
    void setShoeSize(int s) { m_size = s; }
private:
    QString m_name;
    int m_size;
};


int shoeSize() const
} // MyNamespace
{
return m_size;
}
void setShoeSize(int s)
{
m_size = s;
}
 
private:
QString m_name;
int m_size;
};
}


#include "main.moc"
#include "main.moc"
Line 50: Line 32:
using namespace MyNamespace;
using namespace MyNamespace;


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


Text {
Text {
text: myPerson.name
    text: myPerson.name


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

Latest revision as of 12:33, 28 November 2016

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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& n) { m_name = n; }
    int shoeSize() const { return m_size; }
    void setShoeSize(int s) { m_size = s; }
private:
    QString m_name;
    int m_size;
};

} // MyNamespace

#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 *)&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
    }
}