本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873)
接上文[](http://blog.csdn.net/cloud_castle/article/details/37355839)[Qt5官方demo解析集23——Extending QML - Inheritance and Coercion Example](http://blog.csdn.net/cloud_castle/article/details/37363547)
有時我們看到某個QML類型的聲明中,某些數據并沒有放在屬性 + :后面,它們實際上屬于這個類型的默認屬性。而這個demo則向我們介紹了這個技術。
這個例子與上一篇幾乎沒有什么改動,除了兩個小地方。
第一處在QML描述文件中example.qml:
~~~
import People 1.0
// ![0]
BirthdayParty {
host: Boy {
name: "Bob Jones"
shoeSize: 12
}
Boy { name: "Leo Hodges" } // 與前面不同的是,這三個Person并沒有被放置在guests中
Boy { name: "Jack Smith" } // 但是它們的值被賦予了該類型的默認屬性"guests"
Girl { name: "Anne Brown" }
}
// ![0]
~~~
另一個地方則是我們BirthdayParty類的聲明,BirthdayParty.h:
~~~
#ifndef BIRTHDAYPARTY_H
#define BIRTHDAYPARTY_H
#include <QObject>
#include <QQmlListProperty>
#include "person.h"
// ![0]
class BirthdayParty : public QObject
{
Q_OBJECT
Q_PROPERTY(Person *host READ host WRITE setHost)
Q_PROPERTY(QQmlListProperty<Person> guests READ guests)
Q_CLASSINFO("DefaultProperty", "guests") // Q_CLASSINFO宏用來為類添加額外的信息
public: // 而這些信息可以被添加到該類的元對象信息中
BirthdayParty(QObject *parent = 0); // 這里為BirthdayParty類添加了一個默認屬性"guests"
Person *host() const;
void setHost(Person *);
QQmlListProperty<Person> guests();
int guestCount() const;
Person *guest(int) const;
private:
Person *m_host;
QList<Person *> m_guests;
};
// ![0]
#endif // BIRTHDAYPARTY_H
~~~
- 前言
- 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