diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-07-06 08:32:25 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-07-06 08:32:25 (GMT) |
commit | d641b621077d5cc81ce7b3de92af9a0f6d47f4d6 (patch) | |
tree | a6d367c55d3d2ec41cf8bf92a439c8cc14c717d8 /examples/declarative/extending | |
parent | 6c7b88af807cbd2b4d824b8fca0f199ae1413432 (diff) | |
download | Qt-d641b621077d5cc81ce7b3de92af9a0f6d47f4d6.zip Qt-d641b621077d5cc81ce7b3de92af9a0f6d47f4d6.tar.gz Qt-d641b621077d5cc81ce7b3de92af9a0f6d47f4d6.tar.bz2 |
Doc
Diffstat (limited to 'examples/declarative/extending')
44 files changed, 1226 insertions, 0 deletions
diff --git a/examples/declarative/extending/adding/adding.pro b/examples/declarative/extending/adding/adding.pro new file mode 100644 index 0000000..c02a35f --- /dev/null +++ b/examples/declarative/extending/adding/adding.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = adding +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp +HEADERS += person.h +RESOURCES += adding.qrc diff --git a/examples/declarative/extending/adding/adding.qrc b/examples/declarative/extending/adding/adding.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/adding/adding.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>example.qml</file> +</qresource> +</RCC> diff --git a/examples/declarative/extending/adding/example.qml b/examples/declarative/extending/adding/example.qml new file mode 100644 index 0000000..5550cec --- /dev/null +++ b/examples/declarative/extending/adding/example.qml @@ -0,0 +1,6 @@ +// ![0] +Person { + name: "Bob Jones" + shoeSize: 12 +} +// ![0] diff --git a/examples/declarative/extending/adding/main.cpp b/examples/declarative/extending/adding/main.cpp new file mode 100644 index 0000000..3d78ded --- /dev/null +++ b/examples/declarative/extending/adding/main.cpp @@ -0,0 +1,22 @@ +#include <QCoreApplication> +#include <QmlEngine> +#include <QmlComponent> +#include <QDebug> +#include "person.h" + +int main(int argc, char ** argv) +{ + QCoreApplication app(argc, argv); + + QmlEngine engine; + QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + Person *person = qobject_cast<Person *>(component.create()); + if (person) { + qWarning() << "The person's name is" << person->name(); + qWarning() << "They wear a" << person->shoeSize() << "sized shoe"; + } else { + qWarning() << "An error occured"; + } + + return 0; +} diff --git a/examples/declarative/extending/adding/person.cpp b/examples/declarative/extending/adding/person.cpp new file mode 100644 index 0000000..4198b63 --- /dev/null +++ b/examples/declarative/extending/adding/person.cpp @@ -0,0 +1,30 @@ +#include "person.h" + +// ![0] +Person::Person(QObject *parent) +: QObject(parent), m_shoeSize(0) +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &n) +{ + m_name = n; +} + +int Person::shoeSize() const +{ + return m_shoeSize; +} + +void Person::setShoeSize(int s) +{ + m_shoeSize = s; +} + +QML_DEFINE_TYPE(Person, Person); +// ![0] diff --git a/examples/declarative/extending/adding/person.h b/examples/declarative/extending/adding/person.h new file mode 100644 index 0000000..3bef2d4 --- /dev/null +++ b/examples/declarative/extending/adding/person.h @@ -0,0 +1,27 @@ +#ifndef PERSON_H +#define PERSON_H + +#include <QObject> +// ![0] +#include <qml.h> + +class Person : public QObject { +Q_OBJECT +Q_PROPERTY(QString name READ name WRITE setName) +Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +public: + Person(QObject *parent = 0); + + QString name() const; + void setName(const QString &); + + int shoeSize() const; + void setShoeSize(int); +private: + QString m_name; + int m_shoeSize; +}; +QML_DECLARE_TYPE(Person); +// ![0] + +#endif // PERSON_H diff --git a/examples/declarative/extending/attached/attached.pro b/examples/declarative/extending/attached/attached.pro new file mode 100644 index 0000000..b0f3fea --- /dev/null +++ b/examples/declarative/extending/attached/attached.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = attached +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp +HEADERS += person.h \ + birthdayparty.h +RESOURCES += attached.qrc diff --git a/examples/declarative/extending/attached/attached.qrc b/examples/declarative/extending/attached/attached.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/attached/attached.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>example.qml</file> +</qresource> +</RCC> diff --git a/examples/declarative/extending/attached/birthdayparty.cpp b/examples/declarative/extending/attached/birthdayparty.cpp new file mode 100644 index 0000000..49ed5a3 --- /dev/null +++ b/examples/declarative/extending/attached/birthdayparty.cpp @@ -0,0 +1,45 @@ +#include "birthdayparty.h" + +BirthdayPartyAttached::BirthdayPartyAttached(QObject *object) +: QObject(object) +{ +} + +QDate BirthdayPartyAttached::rsvp() const +{ + return m_rsvp; +} + +void BirthdayPartyAttached::setRsvp(const QDate &d) +{ + m_rsvp = d; +} + +QML_DEFINE_NOCREATE_TYPE(BirthdayPartyAttached); + +BirthdayParty::BirthdayParty(QObject *parent) +: QObject(parent), m_celebrant(0) +{ +} + +Person *BirthdayParty::celebrant() const +{ + return m_celebrant; +} + +void BirthdayParty::setCelebrant(Person *c) +{ + m_celebrant = c; +} + +QmlList<Person *> *BirthdayParty::guests() +{ + return &m_guests; +} + +BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) +{ + return new BirthdayPartyAttached(object); +} + +QML_DEFINE_TYPE(BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/attached/birthdayparty.h b/examples/declarative/extending/attached/birthdayparty.h new file mode 100644 index 0000000..8249af5 --- /dev/null +++ b/examples/declarative/extending/attached/birthdayparty.h @@ -0,0 +1,45 @@ +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include <QObject> +#include <QDate> +#include <qml.h> +#include "person.h" + +class BirthdayPartyAttached : public QObject +{ +Q_OBJECT +Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp); +public: + BirthdayPartyAttached(QObject *object); + + QDate rsvp() const; + void setRsvp(const QDate &); + +private: + QDate m_rsvp; +}; +QML_DECLARE_TYPE(BirthdayPartyAttached); + +class BirthdayParty : public QObject +{ +Q_OBJECT +Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) +Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_CLASSINFO("DefaultProperty", "guests") +public: + BirthdayParty(QObject *parent = 0); + + Person *celebrant() const; + void setCelebrant(Person *); + + QmlList<Person *> *guests(); + + static BirthdayPartyAttached *qmlAttachedProperties(QObject *); +private: + Person *m_celebrant; + QmlConcreteList<Person *> m_guests; +}; +QML_DECLARE_TYPE(BirthdayParty); + +#endif // BIRTHDAYPARTY_H diff --git a/examples/declarative/extending/attached/example.qml b/examples/declarative/extending/attached/example.qml new file mode 100644 index 0000000..2645eac --- /dev/null +++ b/examples/declarative/extending/attached/example.qml @@ -0,0 +1,27 @@ +BirthdayParty { + celebrant: Boy { + name: "Bob Jones" + shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } + } + + // ![1] + Boy { + name: "Joan Hodges" + BirthdayParty.rsvp: "2009-07-06" + shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } + } + // ![1] + Boy { + name: "Jack Smith" + shoe { size: 8; color: "blue"; brand: "Puma"; price: 19.95 } + } + Girl { + name: "Anne Brown" + BirthdayParty.rsvp: "2009-07-01" + shoe.size: 7 + shoe.color: "red" + shoe.brand: "Marc Jacobs" + shoe.price: 699.99 + } +} + diff --git a/examples/declarative/extending/attached/main.cpp b/examples/declarative/extending/attached/main.cpp new file mode 100644 index 0000000..1f10cd0 --- /dev/null +++ b/examples/declarative/extending/attached/main.cpp @@ -0,0 +1,44 @@ +#include <QCoreApplication> +#include <QmlEngine> +#include <QmlComponent> +#include <QDebug> +#include "birthdayparty.h" +#include "person.h" + +int main(int argc, char ** argv) +{ + QCoreApplication app(argc, argv); + + QmlEngine engine; + QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + + if (qobject_cast<Boy *>(party->celebrant())) + qWarning() << "He is inviting:"; + else + qWarning() << "She is inviting:"; + + for (int ii = 0; ii < party->guests()->count(); ++ii) { + Person *guest = party->guests()->at(ii); + + QDate rsvpDate; + QObject *attached = + qmlAttachedPropertiesObject<BirthdayParty>(guest, false); + if (attached) + rsvpDate = attached->property("rsvp").toDate(); + + if (rsvpDate.isNull()) + qWarning() << " " << guest->name() << "RSVP date: Hasn't RSVP'd"; + else + qWarning() << " " << guest->name() << "RSVP date:" << qPrintable(rsvpDate.toString()); + } + + } else { + qWarning() << "An error occured"; + } + + return 0; +} diff --git a/examples/declarative/extending/attached/person.cpp b/examples/declarative/extending/attached/person.cpp new file mode 100644 index 0000000..48a94e8 --- /dev/null +++ b/examples/declarative/extending/attached/person.cpp @@ -0,0 +1,83 @@ +#include "person.h" + +ShoeDescription::ShoeDescription(QObject *parent) +: QObject(parent), m_size(0), m_price(0) +{ +} + +int ShoeDescription::size() const +{ + return m_size; +} + +void ShoeDescription::setSize(int s) +{ + m_size = s; +} + +QColor ShoeDescription::color() const +{ + return m_color; +} + +void ShoeDescription::setColor(const QColor &c) +{ + m_color = c; +} + +QString ShoeDescription::brand() const +{ + return m_brand; +} + +void ShoeDescription::setBrand(const QString &b) +{ + m_brand = b; +} + +qreal ShoeDescription::price() const +{ + return m_price; +} + +void ShoeDescription::setPrice(qreal p) +{ + m_price = p; +} +QML_DEFINE_NOCREATE_TYPE(ShoeDescription); + +Person::Person(QObject *parent) +: QObject(parent) +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &n) +{ + m_name = n; +} + +ShoeDescription *Person::shoe() +{ + return &m_shoe; +} + +QML_DEFINE_NOCREATE_TYPE(Person); + +Boy::Boy(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Boy, Boy); + +Girl::Girl(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Girl, Girl); diff --git a/examples/declarative/extending/attached/person.h b/examples/declarative/extending/attached/person.h new file mode 100644 index 0000000..07dfe0e --- /dev/null +++ b/examples/declarative/extending/attached/person.h @@ -0,0 +1,67 @@ +#ifndef PERSON_H +#define PERSON_H + +#include <QObject> +#include <QColor> +#include <qml.h> + +class ShoeDescription : public QObject { +Q_OBJECT +Q_PROPERTY(int size READ size WRITE setSize) +Q_PROPERTY(QColor color READ color WRITE setColor) +Q_PROPERTY(QString brand READ brand WRITE setBrand) +Q_PROPERTY(qreal price READ price WRITE setPrice) +public: + ShoeDescription(QObject *parent = 0); + + int size() const; + void setSize(int); + + QColor color() const; + void setColor(const QColor &); + + QString brand() const; + void setBrand(const QString &); + + qreal price() const; + void setPrice(qreal); +private: + int m_size; + QColor m_color; + QString m_brand; + qreal m_price; +}; +QML_DECLARE_TYPE(ShoeDescription); + +class Person : public QObject { +Q_OBJECT +Q_PROPERTY(QString name READ name WRITE setName) +Q_PROPERTY(ShoeDescription *shoe READ shoe); +public: + Person(QObject *parent = 0); + + QString name() const; + void setName(const QString &); + + ShoeDescription *shoe(); +private: + QString m_name; + ShoeDescription m_shoe; +}; +QML_DECLARE_TYPE(Person); + +class Boy : public Person { +Q_OBJECT +public: + Boy(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Boy); + +class Girl : public Person { +Q_OBJECT +public: + Girl(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Girl); + +#endif // PERSON_H diff --git a/examples/declarative/extending/coercion/birthdayparty.cpp b/examples/declarative/extending/coercion/birthdayparty.cpp new file mode 100644 index 0000000..cdff6e1 --- /dev/null +++ b/examples/declarative/extending/coercion/birthdayparty.cpp @@ -0,0 +1,23 @@ +#include "birthdayparty.h" + +BirthdayParty::BirthdayParty(QObject *parent) +: QObject(parent), m_celebrant(0) +{ +} + +Person *BirthdayParty::celebrant() const +{ + return m_celebrant; +} + +void BirthdayParty::setCelebrant(Person *c) +{ + m_celebrant = c; +} + +QmlList<Person *> *BirthdayParty::guests() +{ + return &m_guests; +} + +QML_DEFINE_TYPE(BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/coercion/birthdayparty.h b/examples/declarative/extending/coercion/birthdayparty.h new file mode 100644 index 0000000..2557d1a --- /dev/null +++ b/examples/declarative/extending/coercion/birthdayparty.h @@ -0,0 +1,29 @@ +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include <QObject> +#include <qml.h> +#include "person.h" + +class BirthdayParty : public QObject +{ +Q_OBJECT +// ![0] +Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) +Q_PROPERTY(QmlList<Person *> *guests READ guests) +// ![0] +public: + BirthdayParty(QObject *parent = 0); + + Person *celebrant() const; + void setCelebrant(Person *); + + QmlList<Person *> *guests(); + +private: + Person *m_celebrant; + QmlConcreteList<Person *> m_guests; +}; +QML_DECLARE_TYPE(BirthdayParty); + +#endif // BIRTHDAYPARTY_H diff --git a/examples/declarative/extending/coercion/coercion.pro b/examples/declarative/extending/coercion/coercion.pro new file mode 100644 index 0000000..136b210 --- /dev/null +++ b/examples/declarative/extending/coercion/coercion.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = coercion +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp +HEADERS += person.h \ + birthdayparty.h +RESOURCES += coercion.qrc diff --git a/examples/declarative/extending/coercion/coercion.qrc b/examples/declarative/extending/coercion/coercion.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/coercion/coercion.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>example.qml</file> +</qresource> +</RCC> diff --git a/examples/declarative/extending/coercion/example.qml b/examples/declarative/extending/coercion/example.qml new file mode 100644 index 0000000..8bcb45a --- /dev/null +++ b/examples/declarative/extending/coercion/example.qml @@ -0,0 +1,13 @@ +// ![0] +BirthdayParty { + celebrant: Boy { + name: "Bob Jones" + shoeSize: 12 + } + guests: [ + Boy { name: "Joan Hodges" }, + Boy { name: "Jack Smith" }, + Girl { name: "Anne Brown" } + ] +} +// ![0] diff --git a/examples/declarative/extending/coercion/main.cpp b/examples/declarative/extending/coercion/main.cpp new file mode 100644 index 0000000..7439ba9 --- /dev/null +++ b/examples/declarative/extending/coercion/main.cpp @@ -0,0 +1,30 @@ +#include <QCoreApplication> +#include <QmlEngine> +#include <QmlComponent> +#include <QDebug> +#include "birthdayparty.h" +#include "person.h" + +int main(int argc, char ** argv) +{ + QCoreApplication app(argc, argv); + + QmlEngine engine; + QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + + if (qobject_cast<Boy *>(party->celebrant())) + qWarning() << "He is inviting:"; + else + qWarning() << "She is inviting:"; + for (int ii = 0; ii < party->guests()->count(); ++ii) + qWarning() << " " << party->guests()->at(ii)->name(); + } else { + qWarning() << "An error occured"; + } + + return 0; +} diff --git a/examples/declarative/extending/coercion/person.cpp b/examples/declarative/extending/coercion/person.cpp new file mode 100644 index 0000000..4605db9 --- /dev/null +++ b/examples/declarative/extending/coercion/person.cpp @@ -0,0 +1,46 @@ +#include "person.h" + +Person::Person(QObject *parent) +: QObject(parent), m_shoeSize(0) +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &n) +{ + m_name = n; +} + +int Person::shoeSize() const +{ + return m_shoeSize; +} + +void Person::setShoeSize(int s) +{ + m_shoeSize = s; +} + +// ![0] +QML_DEFINE_NOCREATE_TYPE(Person); +// ![0] + +// ![1] +Boy::Boy(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Boy, Boy); + +Girl::Girl(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Girl, Girl); +// ![1] diff --git a/examples/declarative/extending/coercion/person.h b/examples/declarative/extending/coercion/person.h new file mode 100644 index 0000000..de14019 --- /dev/null +++ b/examples/declarative/extending/coercion/person.h @@ -0,0 +1,41 @@ +#ifndef PERSON_H +#define PERSON_H + +#include <QObject> +#include <qml.h> + +class Person : public QObject { +Q_OBJECT +Q_PROPERTY(QString name READ name WRITE setName) +Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +public: + Person(QObject *parent = 0); + + QString name() const; + void setName(const QString &); + + int shoeSize() const; + void setShoeSize(int); +private: + QString m_name; + int m_shoeSize; +}; +QML_DECLARE_TYPE(Person); + +// ![0] +class Boy : public Person { +Q_OBJECT +public: + Boy(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Boy); + +class Girl : public Person { +Q_OBJECT +public: + Girl(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Girl); +// ![0] + +#endif // PERSON_H diff --git a/examples/declarative/extending/default/birthdayparty.cpp b/examples/declarative/extending/default/birthdayparty.cpp new file mode 100644 index 0000000..cdff6e1 --- /dev/null +++ b/examples/declarative/extending/default/birthdayparty.cpp @@ -0,0 +1,23 @@ +#include "birthdayparty.h" + +BirthdayParty::BirthdayParty(QObject *parent) +: QObject(parent), m_celebrant(0) +{ +} + +Person *BirthdayParty::celebrant() const +{ + return m_celebrant; +} + +void BirthdayParty::setCelebrant(Person *c) +{ + m_celebrant = c; +} + +QmlList<Person *> *BirthdayParty::guests() +{ + return &m_guests; +} + +QML_DEFINE_TYPE(BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/default/birthdayparty.h b/examples/declarative/extending/default/birthdayparty.h new file mode 100644 index 0000000..084da10 --- /dev/null +++ b/examples/declarative/extending/default/birthdayparty.h @@ -0,0 +1,30 @@ +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include <QObject> +#include <qml.h> +#include "person.h" + +// ![0] +class BirthdayParty : public QObject +{ +Q_OBJECT +Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) +Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_CLASSINFO("DefaultProperty", "guests") +public: + BirthdayParty(QObject *parent = 0); + + Person *celebrant() const; + void setCelebrant(Person *); + + QmlList<Person *> *guests(); + +private: + Person *m_celebrant; + QmlConcreteList<Person *> m_guests; +}; +// ![0] +QML_DECLARE_TYPE(BirthdayParty); + +#endif // BIRTHDAYPARTY_H diff --git a/examples/declarative/extending/default/default.pro b/examples/declarative/extending/default/default.pro new file mode 100644 index 0000000..0d5d45c --- /dev/null +++ b/examples/declarative/extending/default/default.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = default +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp +HEADERS += person.h \ + birthdayparty.h +RESOURCES += default.qrc diff --git a/examples/declarative/extending/default/default.qrc b/examples/declarative/extending/default/default.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/default/default.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>example.qml</file> +</qresource> +</RCC> diff --git a/examples/declarative/extending/default/example.qml b/examples/declarative/extending/default/example.qml new file mode 100644 index 0000000..4023abb --- /dev/null +++ b/examples/declarative/extending/default/example.qml @@ -0,0 +1,12 @@ +// ![0] +BirthdayParty { + celebrant: Boy { + name: "Bob Jones" + shoeSize: 12 + } + + Boy { name: "Joan Hodges" } + Boy { name: "Jack Smith" } + Girl { name: "Anne Brown" } +} +// ![0] diff --git a/examples/declarative/extending/default/main.cpp b/examples/declarative/extending/default/main.cpp new file mode 100644 index 0000000..7439ba9 --- /dev/null +++ b/examples/declarative/extending/default/main.cpp @@ -0,0 +1,30 @@ +#include <QCoreApplication> +#include <QmlEngine> +#include <QmlComponent> +#include <QDebug> +#include "birthdayparty.h" +#include "person.h" + +int main(int argc, char ** argv) +{ + QCoreApplication app(argc, argv); + + QmlEngine engine; + QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + + if (qobject_cast<Boy *>(party->celebrant())) + qWarning() << "He is inviting:"; + else + qWarning() << "She is inviting:"; + for (int ii = 0; ii < party->guests()->count(); ++ii) + qWarning() << " " << party->guests()->at(ii)->name(); + } else { + qWarning() << "An error occured"; + } + + return 0; +} diff --git a/examples/declarative/extending/default/person.cpp b/examples/declarative/extending/default/person.cpp new file mode 100644 index 0000000..c47bfb7 --- /dev/null +++ b/examples/declarative/extending/default/person.cpp @@ -0,0 +1,42 @@ +#include "person.h" + +Person::Person(QObject *parent) +: QObject(parent), m_shoeSize(0) +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &n) +{ + m_name = n; +} + +int Person::shoeSize() const +{ + return m_shoeSize; +} + +void Person::setShoeSize(int s) +{ + m_shoeSize = s; +} + +QML_DEFINE_NOCREATE_TYPE(Person); + +Boy::Boy(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Boy, Boy); + +Girl::Girl(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Girl, Girl); diff --git a/examples/declarative/extending/default/person.h b/examples/declarative/extending/default/person.h new file mode 100644 index 0000000..872186c --- /dev/null +++ b/examples/declarative/extending/default/person.h @@ -0,0 +1,39 @@ +#ifndef PERSON_H +#define PERSON_H + +#include <QObject> +#include <qml.h> + +class Person : public QObject { +Q_OBJECT +Q_PROPERTY(QString name READ name WRITE setName) +Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +public: + Person(QObject *parent = 0); + + QString name() const; + void setName(const QString &); + + int shoeSize() const; + void setShoeSize(int); +private: + QString m_name; + int m_shoeSize; +}; +QML_DECLARE_TYPE(Person); + +class Boy : public Person { +Q_OBJECT +public: + Boy(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Boy); + +class Girl : public Person { +Q_OBJECT +public: + Girl(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Girl); + +#endif // PERSON_H diff --git a/examples/declarative/extending/grouped/birthdayparty.cpp b/examples/declarative/extending/grouped/birthdayparty.cpp new file mode 100644 index 0000000..cdff6e1 --- /dev/null +++ b/examples/declarative/extending/grouped/birthdayparty.cpp @@ -0,0 +1,23 @@ +#include "birthdayparty.h" + +BirthdayParty::BirthdayParty(QObject *parent) +: QObject(parent), m_celebrant(0) +{ +} + +Person *BirthdayParty::celebrant() const +{ + return m_celebrant; +} + +void BirthdayParty::setCelebrant(Person *c) +{ + m_celebrant = c; +} + +QmlList<Person *> *BirthdayParty::guests() +{ + return &m_guests; +} + +QML_DEFINE_TYPE(BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/grouped/birthdayparty.h b/examples/declarative/extending/grouped/birthdayparty.h new file mode 100644 index 0000000..3d53319 --- /dev/null +++ b/examples/declarative/extending/grouped/birthdayparty.h @@ -0,0 +1,28 @@ +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include <QObject> +#include <qml.h> +#include "person.h" + +class BirthdayParty : public QObject +{ +Q_OBJECT +Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) +Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_CLASSINFO("DefaultProperty", "guests") +public: + BirthdayParty(QObject *parent = 0); + + Person *celebrant() const; + void setCelebrant(Person *); + + QmlList<Person *> *guests(); + +private: + Person *m_celebrant; + QmlConcreteList<Person *> m_guests; +}; +QML_DECLARE_TYPE(BirthdayParty); + +#endif // BIRTHDAYPARTY_H diff --git a/examples/declarative/extending/grouped/example.qml b/examples/declarative/extending/grouped/example.qml new file mode 100644 index 0000000..b9e5e6b --- /dev/null +++ b/examples/declarative/extending/grouped/example.qml @@ -0,0 +1,31 @@ +// ![0] +BirthdayParty { + celebrant: Boy { + name: "Bob Jones" + shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } + } + + Boy { + name: "Joan Hodges" + shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } + } + // ![1] + Boy { + name: "Jack Smith" + shoe { + size: 8 + color: "blue" + brand: "Puma" + price: 19.95 + } + } + // ![1] + Girl { + name: "Anne Brown" + shoe.size: 7 + shoe.color: "red" + shoe.brand: "Marc Jacobs" + shoe.price: 699.99 + } +} +// ![0] diff --git a/examples/declarative/extending/grouped/grouped.pro b/examples/declarative/extending/grouped/grouped.pro new file mode 100644 index 0000000..8fde8e8 --- /dev/null +++ b/examples/declarative/extending/grouped/grouped.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = grouped +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp +HEADERS += person.h \ + birthdayparty.h +RESOURCES += grouped.qrc diff --git a/examples/declarative/extending/grouped/grouped.qrc b/examples/declarative/extending/grouped/grouped.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/grouped/grouped.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>example.qml</file> +</qresource> +</RCC> diff --git a/examples/declarative/extending/grouped/main.cpp b/examples/declarative/extending/grouped/main.cpp new file mode 100644 index 0000000..0c9bb97 --- /dev/null +++ b/examples/declarative/extending/grouped/main.cpp @@ -0,0 +1,40 @@ +#include <QCoreApplication> +#include <QmlEngine> +#include <QmlComponent> +#include <QDebug> +#include "birthdayparty.h" +#include "person.h" + +int main(int argc, char ** argv) +{ + QCoreApplication app(argc, argv); + + QmlEngine engine; + QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + + if (qobject_cast<Boy *>(party->celebrant())) + qWarning() << "He is inviting:"; + else + qWarning() << "She is inviting:"; + + Person *bestShoe = 0; + for (int ii = 0; ii < party->guests()->count(); ++ii) { + Person *guest = party->guests()->at(ii); + qWarning() << " " << guest->name(); + + if (!bestShoe || bestShoe->shoe()->price() < guest->shoe()->price()) + bestShoe = guest; + } + if (bestShoe) + qWarning() << bestShoe->name() << "is wearing the best shoes!"; + + } else { + qWarning() << "An error occured"; + } + + return 0; +} diff --git a/examples/declarative/extending/grouped/person.cpp b/examples/declarative/extending/grouped/person.cpp new file mode 100644 index 0000000..48a94e8 --- /dev/null +++ b/examples/declarative/extending/grouped/person.cpp @@ -0,0 +1,83 @@ +#include "person.h" + +ShoeDescription::ShoeDescription(QObject *parent) +: QObject(parent), m_size(0), m_price(0) +{ +} + +int ShoeDescription::size() const +{ + return m_size; +} + +void ShoeDescription::setSize(int s) +{ + m_size = s; +} + +QColor ShoeDescription::color() const +{ + return m_color; +} + +void ShoeDescription::setColor(const QColor &c) +{ + m_color = c; +} + +QString ShoeDescription::brand() const +{ + return m_brand; +} + +void ShoeDescription::setBrand(const QString &b) +{ + m_brand = b; +} + +qreal ShoeDescription::price() const +{ + return m_price; +} + +void ShoeDescription::setPrice(qreal p) +{ + m_price = p; +} +QML_DEFINE_NOCREATE_TYPE(ShoeDescription); + +Person::Person(QObject *parent) +: QObject(parent) +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &n) +{ + m_name = n; +} + +ShoeDescription *Person::shoe() +{ + return &m_shoe; +} + +QML_DEFINE_NOCREATE_TYPE(Person); + +Boy::Boy(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Boy, Boy); + +Girl::Girl(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(Girl, Girl); diff --git a/examples/declarative/extending/grouped/person.h b/examples/declarative/extending/grouped/person.h new file mode 100644 index 0000000..b83eb5b --- /dev/null +++ b/examples/declarative/extending/grouped/person.h @@ -0,0 +1,69 @@ +#ifndef PERSON_H +#define PERSON_H + +#include <QObject> +#include <QColor> +#include <qml.h> + +class ShoeDescription : public QObject { +Q_OBJECT +Q_PROPERTY(int size READ size WRITE setSize) +Q_PROPERTY(QColor color READ color WRITE setColor) +Q_PROPERTY(QString brand READ brand WRITE setBrand) +Q_PROPERTY(qreal price READ price WRITE setPrice) +public: + ShoeDescription(QObject *parent = 0); + + int size() const; + void setSize(int); + + QColor color() const; + void setColor(const QColor &); + + QString brand() const; + void setBrand(const QString &); + + qreal price() const; + void setPrice(qreal); +private: + int m_size; + QColor m_color; + QString m_brand; + qreal m_price; +}; +QML_DECLARE_TYPE(ShoeDescription); + +class Person : public QObject { +Q_OBJECT +Q_PROPERTY(QString name READ name WRITE setName) +// ![1] +Q_PROPERTY(ShoeDescription *shoe READ shoe); +// ![1] +public: + Person(QObject *parent = 0); + + QString name() const; + void setName(const QString &); + + ShoeDescription *shoe(); +private: + QString m_name; + ShoeDescription m_shoe; +}; +QML_DECLARE_TYPE(Person); + +class Boy : public Person { +Q_OBJECT +public: + Boy(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Boy); + +class Girl : public Person { +Q_OBJECT +public: + Girl(QObject * parent = 0); +}; +QML_DECLARE_TYPE(Girl); + +#endif // PERSON_H diff --git a/examples/declarative/extending/properties/example.qml b/examples/declarative/extending/properties/example.qml new file mode 100644 index 0000000..63bb77b --- /dev/null +++ b/examples/declarative/extending/properties/example.qml @@ -0,0 +1,13 @@ +// ![0] +BirthdayParty { + celebrant: Person { + name: "Bob Jones" + shoeSize: 12 + } + guests: [ + Person { name: "Joan Hodges" }, + Person { name: "Jack Smith" }, + Person { name: "Anne Brown" } + ] +} +// ![0] diff --git a/examples/declarative/extending/properties/main.cpp b/examples/declarative/extending/properties/main.cpp new file mode 100644 index 0000000..7b80914 --- /dev/null +++ b/examples/declarative/extending/properties/main.cpp @@ -0,0 +1,26 @@ +#include <QCoreApplication> +#include <QmlEngine> +#include <QmlComponent> +#include <QDebug> +#include "birthdayparty.h" +#include "person.h" + +int main(int argc, char ** argv) +{ + QCoreApplication app(argc, argv); + + QmlEngine engine; + QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + qWarning() << "They are inviting:"; + for (int ii = 0; ii < party->guests()->count(); ++ii) + qWarning() << " " << party->guests()->at(ii)->name(); + } else { + qWarning() << "An error occured"; + } + + return 0; +} diff --git a/examples/declarative/extending/properties/person.cpp b/examples/declarative/extending/properties/person.cpp new file mode 100644 index 0000000..cf66a30 --- /dev/null +++ b/examples/declarative/extending/properties/person.cpp @@ -0,0 +1,28 @@ +#include "person.h" + +Person::Person(QObject *parent) +: QObject(parent), m_shoeSize(0) +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &n) +{ + m_name = n; +} + +int Person::shoeSize() const +{ + return m_shoeSize; +} + +void Person::setShoeSize(int s) +{ + m_shoeSize = s; +} + +QML_DEFINE_TYPE(Person, Person); diff --git a/examples/declarative/extending/properties/person.h b/examples/declarative/extending/properties/person.h new file mode 100644 index 0000000..2bc98c1 --- /dev/null +++ b/examples/declarative/extending/properties/person.h @@ -0,0 +1,25 @@ +#ifndef PERSON_H +#define PERSON_H + +#include <QObject> +#include <qml.h> + +class Person : public QObject { +Q_OBJECT +Q_PROPERTY(QString name READ name WRITE setName) +Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) +public: + Person(QObject *parent = 0); + + QString name() const; + void setName(const QString &); + + int shoeSize() const; + void setShoeSize(int); +private: + QString m_name; + int m_shoeSize; +}; +QML_DECLARE_TYPE(Person); + +#endif // PERSON_H diff --git a/examples/declarative/extending/properties/properties.pro b/examples/declarative/extending/properties/properties.pro new file mode 100644 index 0000000..6355644 --- /dev/null +++ b/examples/declarative/extending/properties/properties.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = properties +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp +HEADERS += person.h \ + birthdayparty.h +RESOURCES += properties.qrc diff --git a/examples/declarative/extending/properties/properties.qrc b/examples/declarative/extending/properties/properties.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/properties/properties.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>example.qml</file> +</qresource> +</RCC> |