From 6c901f67fb2f2e73fa362e72d985a04fa57cdf48 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Tue, 7 Jul 2009 18:17:33 +1000 Subject: Doc --- doc/src/declarative/extending-examples.qdoc | 51 +++++++ doc/src/declarative/extending.qdoc | 165 ++++++++++++++++++++- examples/declarative/extending/binding/binding.pro | 15 ++ examples/declarative/extending/binding/binding.qrc | 5 + .../extending/binding/birthdayparty.cpp | 66 +++++++++ .../declarative/extending/binding/birthdayparty.h | 61 ++++++++ examples/declarative/extending/binding/example.qml | 35 +++++ .../extending/binding/happybirthday.cpp | 47 ++++++ .../declarative/extending/binding/happybirthday.h | 32 ++++ examples/declarative/extending/binding/main.cpp | 45 ++++++ examples/declarative/extending/binding/person.cpp | 103 +++++++++++++ examples/declarative/extending/binding/person.h | 75 ++++++++++ .../declarative/extending/extended/example.qml | 5 + .../declarative/extending/extended/extended.pro | 11 ++ .../declarative/extending/extended/extended.qrc | 5 + .../declarative/extending/extended/lineedit.cpp | 66 +++++++++ examples/declarative/extending/extended/lineedit.h | 34 +++++ examples/declarative/extending/extended/main.cpp | 22 +++ .../declarative/extending/signal/birthdayparty.cpp | 51 +++++++ .../declarative/extending/signal/birthdayparty.h | 52 +++++++ examples/declarative/extending/signal/example.qml | 30 ++++ examples/declarative/extending/signal/main.cpp | 45 ++++++ examples/declarative/extending/signal/person.cpp | 83 +++++++++++ examples/declarative/extending/signal/person.h | 67 +++++++++ examples/declarative/extending/signal/signal.pro | 13 ++ examples/declarative/extending/signal/signal.qrc | 5 + .../extending/valuesource/birthdayparty.cpp | 61 ++++++++ .../extending/valuesource/birthdayparty.h | 57 +++++++ .../declarative/extending/valuesource/example.qml | 34 +++++ .../extending/valuesource/happybirthday.cpp | 42 ++++++ .../extending/valuesource/happybirthday.h | 36 +++++ .../declarative/extending/valuesource/main.cpp | 45 ++++++ .../declarative/extending/valuesource/person.cpp | 83 +++++++++++ .../declarative/extending/valuesource/person.h | 67 +++++++++ .../extending/valuesource/valuesource.pro | 15 ++ .../extending/valuesource/valuesource.qrc | 5 + src/declarative/qml/qmlcomponent.cpp | 6 +- src/declarative/qml/qmlmetaproperty.cpp | 2 +- 38 files changed, 1635 insertions(+), 7 deletions(-) create mode 100644 examples/declarative/extending/binding/binding.pro create mode 100644 examples/declarative/extending/binding/binding.qrc create mode 100644 examples/declarative/extending/binding/birthdayparty.cpp create mode 100644 examples/declarative/extending/binding/birthdayparty.h create mode 100644 examples/declarative/extending/binding/example.qml create mode 100644 examples/declarative/extending/binding/happybirthday.cpp create mode 100644 examples/declarative/extending/binding/happybirthday.h create mode 100644 examples/declarative/extending/binding/main.cpp create mode 100644 examples/declarative/extending/binding/person.cpp create mode 100644 examples/declarative/extending/binding/person.h create mode 100644 examples/declarative/extending/extended/example.qml create mode 100644 examples/declarative/extending/extended/extended.pro create mode 100644 examples/declarative/extending/extended/extended.qrc create mode 100644 examples/declarative/extending/extended/lineedit.cpp create mode 100644 examples/declarative/extending/extended/lineedit.h create mode 100644 examples/declarative/extending/extended/main.cpp create mode 100644 examples/declarative/extending/signal/birthdayparty.cpp create mode 100644 examples/declarative/extending/signal/birthdayparty.h create mode 100644 examples/declarative/extending/signal/example.qml create mode 100644 examples/declarative/extending/signal/main.cpp create mode 100644 examples/declarative/extending/signal/person.cpp create mode 100644 examples/declarative/extending/signal/person.h create mode 100644 examples/declarative/extending/signal/signal.pro create mode 100644 examples/declarative/extending/signal/signal.qrc create mode 100644 examples/declarative/extending/valuesource/birthdayparty.cpp create mode 100644 examples/declarative/extending/valuesource/birthdayparty.h create mode 100644 examples/declarative/extending/valuesource/example.qml create mode 100644 examples/declarative/extending/valuesource/happybirthday.cpp create mode 100644 examples/declarative/extending/valuesource/happybirthday.h create mode 100644 examples/declarative/extending/valuesource/main.cpp create mode 100644 examples/declarative/extending/valuesource/person.cpp create mode 100644 examples/declarative/extending/valuesource/person.h create mode 100644 examples/declarative/extending/valuesource/valuesource.pro create mode 100644 examples/declarative/extending/valuesource/valuesource.qrc diff --git a/doc/src/declarative/extending-examples.qdoc b/doc/src/declarative/extending-examples.qdoc index 09239c1..4fc1bee 100644 --- a/doc/src/declarative/extending-examples.qdoc +++ b/doc/src/declarative/extending-examples.qdoc @@ -256,3 +256,54 @@ This example builds on: \endlist */ + +/*! +\example declarative/extending/signal +\title Extending QML - Signal Support Example + +This example builds on: +\list +\o \l {Extending QML - Attached Properties Example} +\o \l {Extending QML - Grouped Properties Example} +\o \l {Extending QML - Default Property Example} +\o \l {Extending QML - Inheritance and Coercion Example} +\o \l {Extending QML - Object and List Property Types Example} +\o \l {Extending QML - Adding Types Example} +\endlist + +*/ + +/*! +\example declarative/extending/valuesource +\title Extending QML - Property Value Source Example + +This example builds on: +\list +\o \l {Extending QML - Signal Support Example} +\o \l {Extending QML - Attached Properties Example} +\o \l {Extending QML - Grouped Properties Example} +\o \l {Extending QML - Default Property Example} +\o \l {Extending QML - Inheritance and Coercion Example} +\o \l {Extending QML - Object and List Property Types Example} +\o \l {Extending QML - Adding Types Example} +\endlist + +*/ + +/*! +\example declarative/extending/binding +\title Extending QML - Binding Example + +This example builds on: +\list +\o \l {Extending QML - Property Value Source Example} +\o \l {Extending QML - Signal Support Example} +\o \l {Extending QML - Attached Properties Example} +\o \l {Extending QML - Grouped Properties Example} +\o \l {Extending QML - Default Property Example} +\o \l {Extending QML - Inheritance and Coercion Example} +\o \l {Extending QML - Object and List Property Types Example} +\o \l {Extending QML - Adding Types Example} +\endlist + +*/ diff --git a/doc/src/declarative/extending.qdoc b/doc/src/declarative/extending.qdoc index 0f9148a..ac3dc41 100644 --- a/doc/src/declarative/extending.qdoc +++ b/doc/src/declarative/extending.qdoc @@ -359,15 +359,174 @@ object will only be returned if it has previously been created. \l {Extending QML - Attached Properties Example} shows the complete code used to implement the rsvp attached property. -\section1 Signal support +\section1 Signal Support -\section1 Property Binding +\snippet examples/declarative/extending/signal/example.qml 0 +\snippet examples/declarative/extending/signal/example.qml 1 + +The QML snippet shown above associates the evaluation of a ECMAScript expression +with the emission of a Qt signal. + +All Qt signals on a registered class become available as special "signal +propeties" within QML to which the user can assign a single ECMAScript +expression. The signal property's name is a transformed version of the Qt +signal name: "on" is prepended, and the first letter of the signal name upper +cased. For example, the signal used in the example above has the following +C++ signature: + +\snippet examples/declarative/extending/signal/birthdayparty.h 0 + +In classes with multiple signals with the same name, only the final signal +is accessible as a signal property. Although QML provides an element, +\l Connection, for accessing the other signals it is less elegant. For the best +QML API, class developers should avoid overloading signal names. + +Signal parameters become accessible by name to the assigned script. An +unnamed parameter cannot be accessed, so care should be taken to name all the +signal parameters in the C++ class declaration. The intrinsic types +listed in \l {Adding Types}, as well registered object types are permitted as +signal parameter types. Using other types is not an error, but the parameter +value will not be accessible from script. + +\l {Extending QML - Signal Support Example} shows the complete code used to +implement the onPartyStarted signal property. \section1 Property Value Sources +\snippet examples/declarative/extending/valuesource/example.qml 0 +\snippet examples/declarative/extending/valuesource/example.qml 1 + +The QML snippet shown above assigns a property value to the speaker property. +A property value source generates a value for a property that changes over time. + +Property value sources are most commonly used to do animation. Rather than +constructing an animation object and manually setting the animation's "target" +property, a property value source can be assigned directly to a property of any +type and automatically set up this association. + +The example shown here is rather contrived: the speaker property of the +BirthdayParty object is a string that is printed every time it is assigned and +the HappyBirthday value source generates the lyrics of the song +"Happy Birthday". + +\snippet examples/declarative/extending/valuesource/birthdayparty.h 0 + +Normally, assigning an object to a string property would not be allowed. In +the case of a property value source, rather than assigning the object instance +itself, the QML engine sets up an association between the value source and +the property. + +Property value sources are special types that derive from the +QmlPropertyValueSource base class. This base class contains a single method, +QmlPropertyValueSource::setTarget(), that the QML engine invokes when +associating the property value source with a property. The relevant part of +the HappyBirthday type declaration looks like this: + +\snippet examples/declarative/extending/valuesource/happybirthday.h 0 +\snippet examples/declarative/extending/valuesource/happybirthday.h 1 +\snippet examples/declarative/extending/valuesource/happybirthday.h 2 + +In all other respects, property value sources are regular QML types. They must +be registered with the QML engine using the same macros as other types, and can +contain properties, signals and methods just like other types. + +When a property value source object is assigned to a property, QML first tries +to assign it normally, as though it were a regular QML type. Only if this +assignment fails does the engine call the setTarget() method. This allows +the type to also be used in contexts other than just as a value source. + +\l {Extending QML - Property Value Source Example} shows the complete code used +implement the HappyBirthday property value source. + +\section1 Property Binding + +\snippet examples/declarative/extending/binding/example.qml 0 +\snippet examples/declarative/extending/binding/example.qml 1 + +The QML snippet shown above uses a property binding to ensure the +HappyBirthday's name property remains up to date with the celebrant. + +Property binding is a core feature of QML. In addition to assigning literal +values, property bindings allow the developer to assign an arbitrarily complex +ECMAScript expression that may include dependencies on other property values. +Whenever the expression's result changes - through a change in one of its +constituent values - the expression is automatically reevaluated and +the new result assigned to the property. + +All properties on custom types automatically support property binding. However, +for binding to work correctly, QML must be able to reliably determine when a +property has changed so that it knows to reevaluate any bindings that depend on +the property's value. QML relies on the presence of a +\c {Qt's Property System}{NOTIFY signal} for this determination. + +Here is the celebrant property declaration: + +\snippet examples/declarative/extending/binding/birthdayparty.h 0 + +The NOTIFY attribute is followed by a signal name. It is the responsibility of +the class implementer to ensure that whenever the property's value changes, the +NOTIFY signal is emitted. The signature of the NOTIFY signal is not important to QML. + +To prevent loops or excessive evaluation, developers should ensure that the +signal is only emitted whenever the property's value is actually changed. If +a property, or group of properties, is infrequently used it is permitted to use +the same NOTIFY signal for several properties. This should be done with care to +ensure that performance doesn't suffer. + +To keep QML reliable, if a property does not have a NOTIFY signal, it cannot be +used in a binding expression. However, the property can still be assigned +a binding as QML does not need to monitor the property for change in that +scenario. + +Consider a custom type, \c TestElement, that has two properties, "a" and "b". +Property "a" does not have a NOTIFY signal, and property "b" does have a NOTIFY +signal. + +\code +TestElement { + // This is OK + a: b +} +TestElement { + // Will NOT work + b: a +} +\endcode + +The presence of a NOTIFY signal does incur a small overhead. There are cases +where a property's value is set at object construction time, and does not +subsequently change. The most common case of this is when a type uses +\l {Grouped Properties}, and the grouped property object is allocated once, and +only freed when the object is deleted. In these cases, the CONSTANT attribute +may be added to the property declaration instead of a NOTIFY signal. + +\snippet examples/declarative/extending/binding/person.h 0 + +Extreme care must be taken here or applications using your type may misbehave. +The CONSTANT attribute should only be used for properties whose value is set, +and finalized, only in the class constructor. All other properties that want +to be used in bindings should have a NOTIFY signal instead. + +\l {Extending QML - Binding Example} shows the BirthdayParty example updated to +include NOTIFY signals for use in binding. + \section1 Extension Objects -\section1 Parser Status +\snippet examples/declarative/extending/extended/example.qml 0 + +The QML snippet shown above adds a new property to an existing C++ type without +modifying its source code. + +When integrating existing classes and technology into QML, their APIs will often +need to be tweaked to fit better into the declarative environment. Although +the best results are usually obtained by modifying the original classes +directly, if this is either not possible or is complicated by some other +concerns extension objects allow limited extension possibilities without +direct modifications. + +Extension objects can only add properties. + +\section1 Optimization */ diff --git a/examples/declarative/extending/binding/binding.pro b/examples/declarative/extending/binding/binding.pro new file mode 100644 index 0000000..8298565 --- /dev/null +++ b/examples/declarative/extending/binding/binding.pro @@ -0,0 +1,15 @@ +TEMPLATE = app +TARGET = binding +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp \ + happybirthday.cpp +HEADERS += person.h \ + birthdayparty.h \ + happybirthday.h +RESOURCES += binding.qrc diff --git a/examples/declarative/extending/binding/binding.qrc b/examples/declarative/extending/binding/binding.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/binding/binding.qrc @@ -0,0 +1,5 @@ + + + example.qml + + diff --git a/examples/declarative/extending/binding/birthdayparty.cpp b/examples/declarative/extending/binding/birthdayparty.cpp new file mode 100644 index 0000000..6ede183 --- /dev/null +++ b/examples/declarative/extending/binding/birthdayparty.cpp @@ -0,0 +1,66 @@ +#include "birthdayparty.h" + +BirthdayPartyAttached::BirthdayPartyAttached(QObject *object) +: QObject(object) +{ +} + +QDate BirthdayPartyAttached::rsvp() const +{ + return m_rsvp; +} + +void BirthdayPartyAttached::setRsvp(const QDate &d) +{ + if (d != m_rsvp) { + m_rsvp = d; + emit rsvpChanged(); + } +} + +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) +{ + if (c == m_celebrant) return; + m_celebrant = c; + emit celebrantChanged(); +} + +QmlList *BirthdayParty::guests() +{ + return &m_guests; +} + +void BirthdayParty::startParty() +{ + QTime time = QTime::currentTime(); + emit partyStarted(time); +} + +QString BirthdayParty::speaker() const +{ + return QString(); +} + +void BirthdayParty::setSpeaker(const QString &speak) +{ + qWarning() << qPrintable(speak); +} + +BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) +{ + return new BirthdayPartyAttached(object); +} + +QML_DEFINE_TYPE(BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/binding/birthdayparty.h b/examples/declarative/extending/binding/birthdayparty.h new file mode 100644 index 0000000..6905746 --- /dev/null +++ b/examples/declarative/extending/binding/birthdayparty.h @@ -0,0 +1,61 @@ +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include +#include +#include +#include +#include "person.h" + +class BirthdayPartyAttached : public QObject +{ +Q_OBJECT +Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged); +public: + BirthdayPartyAttached(QObject *object); + + QDate rsvp() const; + void setRsvp(const QDate &); + +signals: + void rsvpChanged(); + +private: + QDate m_rsvp; +}; +QML_DECLARE_TYPE(BirthdayPartyAttached); + +class BirthdayParty : public QObject +{ +Q_OBJECT +// ![0] +Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant NOTIFY celebrantChanged) +// ![0] +Q_PROPERTY(QmlList *guests READ guests) +Q_PROPERTY(QString speaker READ speaker WRITE setSpeaker) +Q_CLASSINFO("DefaultProperty", "guests") +public: + BirthdayParty(QObject *parent = 0); + + Person *celebrant() const; + void setCelebrant(Person *); + + QmlList *guests(); + + QString speaker() const; + void setSpeaker(const QString &); + + static BirthdayPartyAttached *qmlAttachedProperties(QObject *); + + void startParty(); +signals: + void partyStarted(const QTime &time); + void celebrantChanged(); + +private: + Person *m_celebrant; + QmlConcreteList m_guests; +}; +QML_DECLARE_TYPE(BirthdayParty); + +#endif // BIRTHDAYPARTY_H diff --git a/examples/declarative/extending/binding/example.qml b/examples/declarative/extending/binding/example.qml new file mode 100644 index 0000000..02c0229 --- /dev/null +++ b/examples/declarative/extending/binding/example.qml @@ -0,0 +1,35 @@ +// ![0] +BirthdayParty { + id: TheParty + + speaker: HappyBirthday { name: TheParty.celebrant.name } + + celebrant: Boy { + name: "Bob Jones" + shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } + } +// ![0] + onPartyStarted: print("This party started rockin' at " + time); + + + Boy { + name: "Joan Hodges" + BirthdayParty.rsvp: "2009-07-06" + shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } + } + 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 + } + +// ![1] +} +// ![1] diff --git a/examples/declarative/extending/binding/happybirthday.cpp b/examples/declarative/extending/binding/happybirthday.cpp new file mode 100644 index 0000000..81e010e --- /dev/null +++ b/examples/declarative/extending/binding/happybirthday.cpp @@ -0,0 +1,47 @@ +#include "happybirthday.h" +#include + +HappyBirthday::HappyBirthday(QObject *parent) +: QmlPropertyValueSource(parent), m_line(-1) +{ + setName(QString()); + QTimer *timer = new QTimer(this); + QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance())); + timer->start(1000); +} + +void HappyBirthday::setTarget(const QmlMetaProperty &p) +{ + m_target = p; +} + +QString HappyBirthday::name() const +{ + return m_name; +} + +void HappyBirthday::setName(const QString &name) +{ + if (m_name == name) + return; + + m_name = name; + + m_lyrics.clear(); + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday dear " + m_name + ","; + m_lyrics << "Happy birthday to you!"; + m_lyrics << ""; + + emit nameChanged(); +} + +void HappyBirthday::advance() +{ + m_line = (m_line + 1) % m_lyrics.count(); + + m_target.write(m_lyrics.at(m_line)); +} + +QML_DEFINE_TYPE(HappyBirthday, HappyBirthday); diff --git a/examples/declarative/extending/binding/happybirthday.h b/examples/declarative/extending/binding/happybirthday.h new file mode 100644 index 0000000..3039db2 --- /dev/null +++ b/examples/declarative/extending/binding/happybirthday.h @@ -0,0 +1,32 @@ +#ifndef HAPPYBIRTHDAY_H +#define HAPPYBIRTHDAY_H + +#include + +class HappyBirthday : public QmlPropertyValueSource +{ +Q_OBJECT +Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) +public: + HappyBirthday(QObject *parent = 0); + + virtual void setTarget(const QmlMetaProperty &); + + QString name() const; + void setName(const QString &); + +private slots: + void advance(); + +signals: + void nameChanged(); +private: + int m_line; + QStringList m_lyrics; + QmlMetaProperty m_target; + QString m_name; +}; +QML_DECLARE_TYPE(HappyBirthday); + +#endif // HAPPYBIRTHDAY_H + diff --git a/examples/declarative/extending/binding/main.cpp b/examples/declarative/extending/binding/main.cpp new file mode 100644 index 0000000..e808b32 --- /dev/null +++ b/examples/declarative/extending/binding/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#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(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + + if (qobject_cast(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(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()); + } + + party->startParty(); + } else { + qWarning() << "An error occured"; + } + + return app.exec(); +} diff --git a/examples/declarative/extending/binding/person.cpp b/examples/declarative/extending/binding/person.cpp new file mode 100644 index 0000000..149669a --- /dev/null +++ b/examples/declarative/extending/binding/person.cpp @@ -0,0 +1,103 @@ +#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) +{ + if (m_size == s) + return; + + m_size = s; + emit shoeChanged(); +} + +QColor ShoeDescription::color() const +{ + return m_color; +} + +void ShoeDescription::setColor(const QColor &c) +{ + if (m_color == c) + return; + + m_color = c; + emit shoeChanged(); +} + +QString ShoeDescription::brand() const +{ + return m_brand; +} + +void ShoeDescription::setBrand(const QString &b) +{ + if (m_brand == b) + return; + + m_brand = b; + emit shoeChanged(); +} + +qreal ShoeDescription::price() const +{ + return m_price; +} + +void ShoeDescription::setPrice(qreal p) +{ + if (m_price == p) + return; + + m_price = p; + emit shoeChanged(); +} +QML_DEFINE_NOCREATE_TYPE(ShoeDescription); + +Person::Person(QObject *parent) +: QObject(parent) +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &n) +{ + if (m_name == n) + return; + + m_name = n; + emit nameChanged(); +} + +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/binding/person.h b/examples/declarative/extending/binding/person.h new file mode 100644 index 0000000..41513b9 --- /dev/null +++ b/examples/declarative/extending/binding/person.h @@ -0,0 +1,75 @@ +#ifndef PERSON_H +#define PERSON_H + +#include +#include +#include + +class ShoeDescription : public QObject { +Q_OBJECT +Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged) +Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged) +Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged) +Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged) +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); +signals: + void shoeChanged(); + +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 NOTIFY nameChanged) +// ![0] +Q_PROPERTY(ShoeDescription *shoe READ shoe CONSTANT); +// ![0] +public: + Person(QObject *parent = 0); + + QString name() const; + void setName(const QString &); + + ShoeDescription *shoe(); +signals: + void nameChanged(); + +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/extended/example.qml b/examples/declarative/extending/extended/example.qml new file mode 100644 index 0000000..040c324 --- /dev/null +++ b/examples/declarative/extending/extended/example.qml @@ -0,0 +1,5 @@ +// ![0] +QLineEdit { + leftMargin: 20 +} +// ![0] diff --git a/examples/declarative/extending/extended/extended.pro b/examples/declarative/extending/extended/extended.pro new file mode 100644 index 0000000..1182226 --- /dev/null +++ b/examples/declarative/extending/extended/extended.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = extended +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + lineedit.cpp +HEADERS += lineedit.h +RESOURCES += extended.qrc diff --git a/examples/declarative/extending/extended/extended.qrc b/examples/declarative/extending/extended/extended.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/extended/extended.qrc @@ -0,0 +1,5 @@ + + + example.qml + + diff --git a/examples/declarative/extending/extended/lineedit.cpp b/examples/declarative/extending/extended/lineedit.cpp new file mode 100644 index 0000000..fe4fdc3 --- /dev/null +++ b/examples/declarative/extending/extended/lineedit.cpp @@ -0,0 +1,66 @@ +#include "lineedit.h" +#include + +LineEditExtension::LineEditExtension(QObject *object) +: QObject(object), m_lineedit(static_cast(object)) +{ +} + +int LineEditExtension::leftMargin() const +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + return l; +} + +int LineEditExtension::setLeftMargin(int m) +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + m_lineedit->setTextMargins(m, t, r, b); +} + +int LineEditExtension::rightMargin() const +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + return r; +} + +int LineEditExtension::setRightMargin(int m) +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + m_lineedit->setTextMargins(l, t, m, b); +} + +int LineEditExtension::topMargin() const +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + return t; +} + +int LineEditExtension::setTopMargin(int m) +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + m_lineedit->setTextMargins(l, m, r, b); +} + +int LineEditExtension::bottomMargin() const +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + return b; +} + +int LineEditExtension::setBottomMargin(int m) +{ + int l, r, t, b; + m_lineedit->getTextMargins(&l, &t, &r, &b); + m_lineedit->setTextMargins(l, t, r, m); +} + +QML_DECLARE_TYPE(QLineEdit); +QML_DEFINE_EXTENDED_TYPE(QLineEdit, QLineEdit, LineEditExtension); diff --git a/examples/declarative/extending/extended/lineedit.h b/examples/declarative/extending/extended/lineedit.h new file mode 100644 index 0000000..860e042 --- /dev/null +++ b/examples/declarative/extending/extended/lineedit.h @@ -0,0 +1,34 @@ +#ifndef LINEEDIT_H +#define LINEEDIT_H + +#include + +class LineEditExtension : public QObject +{ +Q_OBJECT +Q_PROPERTY(int leftMargin READ leftMargin WRITE setLeftMargin NOTIFY marginsChanged); +Q_PROPERTY(int rightMargin READ rightMargin WRITE setRightMargin NOTIFY marginsChanged); +Q_PROPERTY(int topMargin READ topMargin WRITE setTopMargin NOTIFY marginsChanged); +Q_PROPERTY(int bottomMargin READ bottomMargin WRITE setBottomMargin NOTIFY marginsChanged); +public: + LineEditExtension(QObject *); + + int leftMargin() const; + int setLeftMargin(int); + + int rightMargin() const; + int setRightMargin(int); + + int topMargin() const; + int setTopMargin(int); + + int bottomMargin() const; + int setBottomMargin(int); +signals: + void marginsChanged(); + +private: + QLineEdit *m_lineedit; +}; + +#endif // LINEEDIT_H diff --git a/examples/declarative/extending/extended/main.cpp b/examples/declarative/extending/extended/main.cpp new file mode 100644 index 0000000..1006214 --- /dev/null +++ b/examples/declarative/extending/extended/main.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include + +int main(int argc, char ** argv) +{ + QApplication app(argc, argv); + + QmlEngine engine; + QmlComponent component(&engine, ":example.qml"); + QLineEdit *edit = qobject_cast(component.create()); + + if (edit) { + edit->show(); + return app.exec(); + } else { + qWarning() << "An error occured"; + return 0; + } +} diff --git a/examples/declarative/extending/signal/birthdayparty.cpp b/examples/declarative/extending/signal/birthdayparty.cpp new file mode 100644 index 0000000..ceb5e15 --- /dev/null +++ b/examples/declarative/extending/signal/birthdayparty.cpp @@ -0,0 +1,51 @@ +#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 *BirthdayParty::guests() +{ + return &m_guests; +} + +void BirthdayParty::startParty() +{ + QTime time = QTime::currentTime(); + emit partyStarted(time); +} + +BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) +{ + return new BirthdayPartyAttached(object); +} + +QML_DEFINE_TYPE(BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/signal/birthdayparty.h b/examples/declarative/extending/signal/birthdayparty.h new file mode 100644 index 0000000..14d7c29 --- /dev/null +++ b/examples/declarative/extending/signal/birthdayparty.h @@ -0,0 +1,52 @@ +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include +#include +#include +#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 *guests READ guests) +Q_CLASSINFO("DefaultProperty", "guests") +public: + BirthdayParty(QObject *parent = 0); + + Person *celebrant() const; + void setCelebrant(Person *); + + QmlList *guests(); + + static BirthdayPartyAttached *qmlAttachedProperties(QObject *); + + void startParty(); +// ![0] +signals: + void partyStarted(const QTime &time); +// ![0] + +private: + Person *m_celebrant; + QmlConcreteList m_guests; +}; +QML_DECLARE_TYPE(BirthdayParty); + +#endif // BIRTHDAYPARTY_H diff --git a/examples/declarative/extending/signal/example.qml b/examples/declarative/extending/signal/example.qml new file mode 100644 index 0000000..f3e4747 --- /dev/null +++ b/examples/declarative/extending/signal/example.qml @@ -0,0 +1,30 @@ +// ![0] +BirthdayParty { + onPartyStarted: print("This party started rockin' at " + time); +// ![0] + + celebrant: Boy { + name: "Bob Jones" + shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } + } + + Boy { + name: "Joan Hodges" + BirthdayParty.rsvp: "2009-07-06" + shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } + } + 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 + } +// ![1] +} +// ![1] diff --git a/examples/declarative/extending/signal/main.cpp b/examples/declarative/extending/signal/main.cpp new file mode 100644 index 0000000..181614d --- /dev/null +++ b/examples/declarative/extending/signal/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#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(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + + if (qobject_cast(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(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()); + } + + party->startParty(); + } else { + qWarning() << "An error occured"; + } + + return 0; +} diff --git a/examples/declarative/extending/signal/person.cpp b/examples/declarative/extending/signal/person.cpp new file mode 100644 index 0000000..48a94e8 --- /dev/null +++ b/examples/declarative/extending/signal/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/signal/person.h b/examples/declarative/extending/signal/person.h new file mode 100644 index 0000000..07dfe0e --- /dev/null +++ b/examples/declarative/extending/signal/person.h @@ -0,0 +1,67 @@ +#ifndef PERSON_H +#define PERSON_H + +#include +#include +#include + +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/signal/signal.pro b/examples/declarative/extending/signal/signal.pro new file mode 100644 index 0000000..30e413f --- /dev/null +++ b/examples/declarative/extending/signal/signal.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = signal +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp +HEADERS += person.h \ + birthdayparty.h +RESOURCES += signal.qrc diff --git a/examples/declarative/extending/signal/signal.qrc b/examples/declarative/extending/signal/signal.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/signal/signal.qrc @@ -0,0 +1,5 @@ + + + example.qml + + diff --git a/examples/declarative/extending/valuesource/birthdayparty.cpp b/examples/declarative/extending/valuesource/birthdayparty.cpp new file mode 100644 index 0000000..9132ee7 --- /dev/null +++ b/examples/declarative/extending/valuesource/birthdayparty.cpp @@ -0,0 +1,61 @@ +#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 *BirthdayParty::guests() +{ + return &m_guests; +} + +void BirthdayParty::startParty() +{ + QTime time = QTime::currentTime(); + emit partyStarted(time); +} + +QString BirthdayParty::speaker() const +{ + return QString(); +} + +void BirthdayParty::setSpeaker(const QString &speak) +{ + qWarning() << qPrintable(speak); +} + +BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) +{ + return new BirthdayPartyAttached(object); +} + +QML_DEFINE_TYPE(BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/valuesource/birthdayparty.h b/examples/declarative/extending/valuesource/birthdayparty.h new file mode 100644 index 0000000..fd25f28 --- /dev/null +++ b/examples/declarative/extending/valuesource/birthdayparty.h @@ -0,0 +1,57 @@ +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include +#include +#include +#include +#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 *guests READ guests) +// ![0] +Q_PROPERTY(QString speaker READ speaker WRITE setSpeaker) +// ![0] +Q_CLASSINFO("DefaultProperty", "guests") +public: + BirthdayParty(QObject *parent = 0); + + Person *celebrant() const; + void setCelebrant(Person *); + + QmlList *guests(); + + QString speaker() const; + void setSpeaker(const QString &); + + static BirthdayPartyAttached *qmlAttachedProperties(QObject *); + + void startParty(); +signals: + void partyStarted(const QTime &time); + +private: + Person *m_celebrant; + QmlConcreteList m_guests; +}; +QML_DECLARE_TYPE(BirthdayParty); + +#endif // BIRTHDAYPARTY_H diff --git a/examples/declarative/extending/valuesource/example.qml b/examples/declarative/extending/valuesource/example.qml new file mode 100644 index 0000000..033d9c4 --- /dev/null +++ b/examples/declarative/extending/valuesource/example.qml @@ -0,0 +1,34 @@ +// ![0] +BirthdayParty { + speaker: HappyBirthday { name: "Bob Jones" } +// ![0] + + onPartyStarted: print("This party started rockin' at " + time); + + + celebrant: Boy { + name: "Bob Jones" + shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } + } + + Boy { + name: "Joan Hodges" + BirthdayParty.rsvp: "2009-07-06" + shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } + } + 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 + } + +// ![1] +} +// ![1] diff --git a/examples/declarative/extending/valuesource/happybirthday.cpp b/examples/declarative/extending/valuesource/happybirthday.cpp new file mode 100644 index 0000000..1ed309c --- /dev/null +++ b/examples/declarative/extending/valuesource/happybirthday.cpp @@ -0,0 +1,42 @@ +#include "happybirthday.h" +#include + +HappyBirthday::HappyBirthday(QObject *parent) +: QmlPropertyValueSource(parent), m_line(-1) +{ + setName(QString()); + QTimer *timer = new QTimer(this); + QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance())); + timer->start(1000); +} + +void HappyBirthday::setTarget(const QmlMetaProperty &p) +{ + m_target = p; +} + +QString HappyBirthday::name() const +{ + return m_name; +} + +void HappyBirthday::setName(const QString &name) +{ + m_name = name; + + m_lyrics.clear(); + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday to you,"; + m_lyrics << "Happy birthday dear " + m_name + ","; + m_lyrics << "Happy birthday to you!"; + m_lyrics << ""; +} + +void HappyBirthday::advance() +{ + m_line = (m_line + 1) % m_lyrics.count(); + + m_target.write(m_lyrics.at(m_line)); +} + +QML_DEFINE_TYPE(HappyBirthday, HappyBirthday); diff --git a/examples/declarative/extending/valuesource/happybirthday.h b/examples/declarative/extending/valuesource/happybirthday.h new file mode 100644 index 0000000..e0d4912 --- /dev/null +++ b/examples/declarative/extending/valuesource/happybirthday.h @@ -0,0 +1,36 @@ +#ifndef HAPPYBIRTHDAY_H +#define HAPPYBIRTHDAY_H + +#include + +// ![0] +class HappyBirthday : public QmlPropertyValueSource +{ +Q_OBJECT +// ![0] +Q_PROPERTY(QString name READ name WRITE setName) +// ![1] +public: + HappyBirthday(QObject *parent = 0); + + virtual void setTarget(const QmlMetaProperty &); +// ![1] + + QString name() const; + void setName(const QString &); + +private slots: + void advance(); + +private: + int m_line; + QStringList m_lyrics; + QmlMetaProperty m_target; + QString m_name; +// ![2] +}; +// ![2] +QML_DECLARE_TYPE(HappyBirthday); + +#endif // HAPPYBIRTHDAY_H + diff --git a/examples/declarative/extending/valuesource/main.cpp b/examples/declarative/extending/valuesource/main.cpp new file mode 100644 index 0000000..e808b32 --- /dev/null +++ b/examples/declarative/extending/valuesource/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#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(component.create()); + + if (party && party->celebrant()) { + qWarning() << party->celebrant()->name() << "is having a birthday!"; + + if (qobject_cast(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(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()); + } + + party->startParty(); + } else { + qWarning() << "An error occured"; + } + + return app.exec(); +} diff --git a/examples/declarative/extending/valuesource/person.cpp b/examples/declarative/extending/valuesource/person.cpp new file mode 100644 index 0000000..48a94e8 --- /dev/null +++ b/examples/declarative/extending/valuesource/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/valuesource/person.h b/examples/declarative/extending/valuesource/person.h new file mode 100644 index 0000000..07dfe0e --- /dev/null +++ b/examples/declarative/extending/valuesource/person.h @@ -0,0 +1,67 @@ +#ifndef PERSON_H +#define PERSON_H + +#include +#include +#include + +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/valuesource/valuesource.pro b/examples/declarative/extending/valuesource/valuesource.pro new file mode 100644 index 0000000..9e54448 --- /dev/null +++ b/examples/declarative/extending/valuesource/valuesource.pro @@ -0,0 +1,15 @@ +TEMPLATE = app +TARGET = valuesource +DEPENDPATH += . +INCLUDEPATH += . +QT += declarative + +# Input +SOURCES += main.cpp \ + person.cpp \ + birthdayparty.cpp \ + happybirthday.cpp +HEADERS += person.h \ + birthdayparty.h \ + happybirthday.h +RESOURCES += valuesource.qrc diff --git a/examples/declarative/extending/valuesource/valuesource.qrc b/examples/declarative/extending/valuesource/valuesource.qrc new file mode 100644 index 0000000..e2fa01d --- /dev/null +++ b/examples/declarative/extending/valuesource/valuesource.qrc @@ -0,0 +1,5 @@ + + + example.qml + + diff --git a/src/declarative/qml/qmlcomponent.cpp b/src/declarative/qml/qmlcomponent.cpp index 3474487..a9f5442 100644 --- a/src/declarative/qml/qmlcomponent.cpp +++ b/src/declarative/qml/qmlcomponent.cpp @@ -270,18 +270,18 @@ QmlComponent::QmlComponent(QmlEngine *engine, const QUrl &url, QObject *parent) } /*! - Create a QmlComponent from the given \a url and give it the specified + Create a QmlComponent from the given \a fileName and give it the specified \a parent and \a engine. \sa loadUrl() */ -QmlComponent::QmlComponent(QmlEngine *engine, const QString &url, +QmlComponent::QmlComponent(QmlEngine *engine, const QString &fileName, QObject *parent) : QObject(*(new QmlComponentPrivate), parent) { Q_D(QmlComponent); d->engine = engine; - loadUrl(QUrl(url)); + loadUrl(QUrl::fromLocalFile(fileName)); } /*! diff --git a/src/declarative/qml/qmlmetaproperty.cpp b/src/declarative/qml/qmlmetaproperty.cpp index ee24074..e158adf 100644 --- a/src/declarative/qml/qmlmetaproperty.cpp +++ b/src/declarative/qml/qmlmetaproperty.cpp @@ -578,7 +578,7 @@ void QmlMetaPropertyPrivate::findSignalInt(QObject *obj, const QString &name) const QMetaObject *mo = obj->metaObject(); int methods = mo->methodCount(); - for (int ii = 0; ii < methods; ++ii) { + for (int ii = methods - 1; ii >= 0; --ii) { QMetaMethod method = mo->method(ii); QString methodName = QLatin1String(method.signature()); int idx = methodName.indexOf(QLatin1Char('(')); -- cgit v0.12