QML之使用C++自定义QML类型(三)

    今天继续学习向QML注册自定义C++类型,其实QML的API也不外乎就这样写成的,不过现在还没时间去看源码。

    今天的代码是增加两个Person的子类,Boy和Girl,并分别注册到QML.

  

    person.h

   #ifndef PERSON_H #define PERSON_H #include class Person : public QObject { Q_OBJECT //声明属性,用于在QML组件中直接访问 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; }; class Boy : public Person { Q_OBJECT public: Boy(QObject * parent = 0); }; class Girl : public Person { Q_OBJECT public: Girl(QObject * parent = 0); }; #endif // PERSON_H   

 

   person.cpp

   #include "person.h" Person::Person(QObject *parent) : QObject(parent), m_shoeSize(0) { } void Person::setName(const QString & name) { m_name = name; } QString Person::name() const { return m_name; } void Person::setShoeSize(int size) { m_shoeSize = size; } int Person::shoeSize() const { return m_shoeSize; } Boy::Boy(QObject *parent) : Person(parent) { } Girl::Girl(QObject *parent) : Person(parent) { }

 

   birthdayparty.h

   #ifndef BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H #include #include #include "person.h" class BirthdayParty : public QObject { Q_OBJECT //声明属性,用于在QML组件中直接访问 Q_PROPERTY(Person *host READ host WRITE setHost) Q_PROPERTY(QDeclarativeListProperty guests READ guests) public: BirthdayParty(QObject *parent = 0); Person *host() const; void setHost(Person *); QDeclarativeListProperty guests(); int guestCount() const; Person *guest(int) const; private: Person * m_host; QList m_guests; }; #endif // BIRTHDAYPARTY_H

 

   birthdayparty.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 BirthdayParty::guests() { return QDeclarativeListProperty(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 #include #include #include #include "birthdayparty.h" #include "person.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); //向QML注册BirthdayParty,Person,Boy,Girl qmlRegisterType("People", 1, 0, "BirthdayParty"); qmlRegisterType(); qmlRegisterType("People", 1, 0, "Boy"); qmlRegisterType("People", 1, 0, "Girl"); QDeclarativeEngine engine; QDeclarativeComponent component(&engine, QUrl("qrc:/example.qml")); //创建BirthdayParty的QML组件并强转为C++的BirthdayParty类型 BirthdayParty *party = qobject_cast(component.create()); if(party && party->host()) { qWarning() << party->host()->name() <<"is having a birthday!"; if(qobject_cast(party->host())) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; for(int i = 0; i < party->guestCount(); ++i) { qWarning() << " " << party->guest(i)->name(); } } else { qWarning() << component.errors(); } return a.exec(); }

 

   example.qml

   import People 1.0 BirthdayParty { host : Boy { name: "Lavy Liu" shoeSize: 12 } guests: [ Boy { name: "Sglaze"; shoeSize: 18}, Girl { name: "Lucy"; shoeSize: 20}, Girl { name: "Lily"} ] }

 

   输出:

   "Lavy Liu" is having a birthday!

  He is inviting:

 "Sglaze"

 "Lucy" 

 "Lily"

 

 

  N9已经正式发布了,苦苦等了一年啊。

  今天更新了QT,安装了Harmattan,但是模拟器还是跑不起,唉。估计又是悲剧的NVidia惹的祸,还有我这电脑不支持VT。

   报错:

    :-1: error: Deployment failed: Qemu was not running. It has now been started up for you, but it will take a bit of time until it is ready.

   希望有哪位牛人帮忙解决下。

你可能感兴趣的:(Meego)