下面的例子演示如何向利用C++自定义QML类型及自定义类型List
在Person类中用到name 和shoeSize属性,故在person.h中使用:
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
在BirthdayParty中类中用到Person类对象和一个以Person为元素的List对象 guests 作为属性且guests只读,故使用:
Q_PROPERTY(Person *host READ host WRITE setHost) Q_PROPERTY(QDeclarativeListProperty<Person> guests READ guests)
person.h ---------------------------------------- #ifndef PERSON_H #define PERSON_H #include <QObject> 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; }; #endif // PERSON_H
person.cpp -------------------------------------------- #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; }
birthday.h ----------------------------------------------- #ifndef BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H #include <QObject> #include <QtDeclarative/QDeclarativeListProperty> #include "person.h" class BirthdayParty : public QObject { Q_OBJECT Q_PROPERTY(Person *host READ host WRITE setHost) Q_PROPERTY(QDeclarativeListProperty<Person> guests READ guests) public: BirthdayParty(QObject *parent = 0); Person *host() const; void setHost(Person *); QDeclarativeListProperty<Person> guests(); int guestCount() const; Person *guest(int) const; private: Person *m_host; QList<Person *> m_guests; }; #endif // BIRTHDAYPARTY_H
birthday.cpp ------------------------------------------ #include "birthdayparty.h" BirthdayParty::BirthdayParty(QObject *parent) : QObject(parent), m_host(0) { } Person *BirthdayParty::host() const { return m_host; } void BirthdayParty::setHost(Person * c) { m_host = c; } QDeclarativeListProperty<Person> BirthdayParty::guests() { return QDeclarativeListProperty<Person>(this, m_guests); } int BirthdayParty::guestCount() const { return m_guests.count(); } Person *BirthdayParty::guest(int index) const { return m_guests.at(index); }
main.cpp ------------------------------------------ #include <QtGui/QApplication> #include <QDeclarativeEngine> #include <QDeclarativeComponent> #include <QDebug> #include "birthdayparty.h" #include "person.h" int main(int argc, char ** argv) { QApplication app(argc, argv); qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty"); qmlRegisterType<Person>("People", 1,0, "Person"); QDeclarativeEngine engine; QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml")); BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); if (party && party->host()) { qWarning() << party->host()->name() << "is having a birthday!"; qWarning() << "They are inviting:"; for (int i = 0; i < party->guestCount(); ++i) qWarning() << " " << party->guest(i)->name() << party->guest(i)->shoeSize(); } else { qWarning() << component.errors(); } return app.exec(); }
example.qml ----------------------------------------------- import People 1.0 BirthdayParty { host : Person { name: "Lavy Liu" shoeSize: 12 } guests: [ Person { name: "Sglaze"; shoeSize: 18}, Person { name: "liuhongwei"; shoeSize: 23}, Person { name: "liuhw"} ] }
输出:
"Lavy Liu" is having a birthday!
They are inviting:
"Sglaze" 18
"liuhongwei" 23
"liuhw" 0