diff options
Diffstat (limited to 'examples/declarative/extending/coercion')
8 files changed, 202 insertions, 0 deletions
diff --git a/examples/declarative/extending/coercion/birthdayparty.cpp b/examples/declarative/extending/coercion/birthdayparty.cpp new file mode 100644 index 0000000..ea97f548 --- /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(People, 1, 0, 0, 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..5090782 --- /dev/null +++ b/examples/declarative/extending/coercion/example.qml @@ -0,0 +1,15 @@ +import Qt 4.6 + +// ![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..75846fa --- /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, ":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..d075851 --- /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(People, 1, 0, 0, Boy, Boy); + +Girl::Girl(QObject * parent) +: Person(parent) +{ +} + +QML_DEFINE_TYPE(People, 1, 0, 0, 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 |