本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873)
接上文[Qt5官方demo解析集21——Extending QML - Adding Types Example](http://blog.csdn.net/cloud_castle/article/details/37055429)
在上一個例子中我們基于C++創建了一個自定義的QML類型,接下來,我們將該類作為另一個類的屬性類型,定義了另一個birthdayparty類。這個例子與[Qt5官方demo解析集19——Chapter 5: Using List Property Types](http://blog.csdn.net/cloud_castle/article/details/36898495)是十分相似的,只不過在這個例子中只提供了對列表屬性的訪問能力。[](http://blog.csdn.net/cloud_castle/article/details/36898495)
項目文件如下:

Person的定義和聲明與上一篇博文中所貼出的一致,因此我們先看看birthdayparty.h:
~~~
#ifndef BIRTHDAYPARTY_H
#define BIRTHDAYPARTY_H
#include <QObject>
#include <QQmlListProperty> // 這個類型我們在解析集19中有討論過,它用來提供帶有列表成員的屬性
#include "person.h"
// ![0]
class BirthdayParty : public QObject
{
Q_OBJECT
// ![0]
// ![1]
Q_PROPERTY(Person *host READ host WRITE setHost) // host屬性,指向一個Person對象
// ![1]
// ![2]
Q_PROPERTY(QQmlListProperty<Person> guests READ guests) // guests屬性,指向一個Person列表
// ![2]
// ![3]
public:
BirthdayParty(QObject *parent = 0);
Person *host() const;
void setHost(Person *);
QQmlListProperty<Person> guests(); // 上面是屬性的讀寫函數
int guestCount() const; // 下面是自定義的用來返回客人數與讀取某個客人的函數
Person *guest(int) const;
private:
Person *m_host;
QList<Person *> m_guests; // 對應的需要將m_guests定義成QList<Person *>類型的成員列表
};
// ![3]
#endif // BIRTHDAYPARTY_H
~~~
birthdayparty.cpp:
~~~
#include "birthdayparty.h"
BirthdayParty::BirthdayParty(QObject *parent)
: QObject(parent), m_host(0) // 初始化參數賦值,這個0相當于NULL
{
}
// ![0]
Person *BirthdayParty::host() const
{
return m_host;
}
void BirthdayParty::setHost(Person *c)
{
m_host = c;
}
QQmlListProperty<Person> BirthdayParty::guests()
{
return QQmlListProperty<Person>(this, m_guests); // 由一個QList得到QQmlListProperty值,函數原型如下
}
int BirthdayParty::guestCount() const // 數據成員一般都為私有,要調用它的函數,我們需要提供一個公共的接口
{
return m_guests.count();
}
Person *BirthdayParty::guest(int index) const
{
return m_guests.at(index);
}
~~~

example.qml:
~~~
import People 1.0 // 由于在main函數中將Person與Birthdayparty的名稱空間均注冊為People 1.0,導入它我們可以同時使用這兩個類型
// ![0]
BirthdayParty {
host: Person { // host參數為單Person
name: "Bob Jones"
shoeSize: 12
}
guests: [ // guests參數為多Person
Person { name: "Leo Hodges" },
Person { name: "Jack Smith" },
Person { name: "Anne Brown" }
]
}
// ![0]
~~~
main.cpp:
~~~
#include <QCoreApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include "birthdayparty.h"
#include "person.h"
int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv);
//![register list]
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty"); // QML類型注冊
qmlRegisterType<Person>("People", 1,0, "Person");
//![register list]
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:example.qml"));
BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create());
if (party && party->host()) { // 由于host為"Bob Jones",該if得以進入
qWarning() << party->host()->name() << "is having a birthday!";
qWarning() << "They are inviting:";
for (int ii = 0; ii < party->guestCount(); ++ii) // 調用公共接口得到客人數
qWarning() << " " << party->guest(ii)->name(); // 逐一訪問其姓名
} else {
qWarning() << component.errors();
}
return 0;
}
~~~
- 前言
- 1——Fortune Server/Client
- 2——Multicast Sender/Receiverz
- 3——Broadcast Sender/Receiver
- 4——Blocking Fortune Client
- 5——Threaded Fortune Server
- 5(總結)——Fortune例程的各個實現區別
- 6——Loopback Example
- 7——Analog Clock Example
- 8——Shaped Clock Example
- 9——Analog Clock Window Example
- 10——Qt Quick Particles Examples - Emitters
- 11——Qt Quick Particles Examples - Affectors
- 12——Qt Quick Particles Examples - CustomParticles
- 13——Qt Quick Particles Examples - Image Particles
- 14——Qt Quick Particles Examples - System
- 15——Chapter 1: Creating a New Type
- 16——Chapter 2: Connecting to C++ Methods and Signals
- 17——Chapter 3: Adding Property Bindings
- 18——Chapter 4: Using Custom Property Types
- 19——Chapter 5: Using List Property Types
- 20——Chapter 6: Writing an Extension Plugin
- 21——Extending QML - Adding Types Example
- 22——Extending QML - Object and List Property Types Example
- 23——Extending QML - Inheritance and Coercion Example
- 24——Extending QML - Default Property Example
- 25——Extending QML - Methods Example
- 26——Extending QML - Grouped Properties Example
- 27——Extending QML - Attached Properties Example
- 28——Extending QML - Signal Support Example
- 29——Extending QML - Property Value Source Example
- 30——Extending QML - Binding Example
- 31——StocQt
- 32——Qt Quick Examples - Threading
- 33——Qt Quick Examples - Window and Screen
- 34——Concentric Circles Example
- 35——Music Player
- 36——Wiggly Example
- 37——Vector Deformation