本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873)
接上文[Qt5官方demo解析集25——Extending QML - Methods Example](http://blog.csdn.net/cloud_castle/article/details/37391595)
如果之前看過了我前面介紹粒子系統的朋友,應該對
velocity:?AngleDirection?{angleVariation:?360;?magnitude:?80;?magnitudeVariation:?40} ?
這樣的屬性設置格式屢見不鮮了,它實際是將一個AngleDirection類型作為velocity的屬性值,這樣我們就可以通過設置AngleDirection的屬性值來達到對velocity更復雜的控制。
在這個例子中,Qt 向我們展示了一種更巧妙的做法 —— 直接使用AngleDirection的屬性。
可能你之前經常會使用到font.family: "Ubuntu" 或是 font.pixelSize: 24 ,實際上這里的font 也是一個集合屬性。
還是先把我們的項目文件介紹一下:

兩個自定義的C++類:Person與BirthdayParty,資源文件中是我們的QML文件example.qml。
為了有一個直觀的印象,我們先來看看qml的實現:
example.qml:
~~~
import People 1.0
import QtQuick 2.0 // For QColor
// ![0]
BirthdayParty {
host: Boy {
name: "Bob Jones"
shoe { size: 12; color: "white"; brand: "Bikey"; price: 90.0 } // QML語法基礎保證冒號為一個賦值運算
} // 而這個語句保證了屬性賦值在大括號內部進行
Boy {
name: "Leo Hodges"
//![grouped]
shoe { size: 10; color: "black"; brand: "Thebok"; price: 59.95 }
//![grouped]
}
// ![1]
Boy {
name: "Jack Smith"
shoe {
size: 8
color: "blue"
brand: "Luma"
price: 19.95
}
}
// ![1]
Girl {
name: "Anne Brown"
//![ungrouped]
shoe.size: 7 // 大括號拆開來實際就變成這幾個單獨的語句
shoe.color: "red"
shoe.brand: "Job Macobs"
shoe.price: 699.99
//![ungrouped]
}
}
// ![0]
~~~
這種賦值方法是如何實現的呢,來看C++的定義:
BirthdayParty提供了生日派對的框架,這個類沒有變化。birthdayparty.h:
~~~
#ifndef BIRTHDAYPARTY_H
#define BIRTHDAYPARTY_H
#include <QObject>
#include <QQmlListProperty>
#include "person.h"
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")
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;
};
~~~
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;
}
QQmlListProperty<Person> BirthdayParty::guests()
{
return QQmlListProperty<Person>(this, m_guests);
}
int BirthdayParty::guestCount() const
{
return m_guests.count();
}
Person *BirthdayParty::guest(int index) const
{
return m_guests.at(index);
}
~~~
接下來,Person文件中定義了一個額外的類ShoeDescription用來為shoe屬性提供描述。
person.h:
~~~
#ifndef PERSON_H
#define PERSON_H
#include <QObject>
#include <QColor>
class ShoeDescription : public QObject // 這個類的作用類似我們前面所說的AngleDirection,用這個自定義類型定義shoe屬性
{
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;
};
class Person : public QObject // Person類定義
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
// ![1]
Q_PROPERTY(ShoeDescription *shoe READ shoe) // 將shoe屬性設置為只讀,否則它需要被冒號賦值
// ![1]
public:
Person(QObject *parent = 0);
QString name() const;
void setName(const QString &);
ShoeDescription *shoe();
private:
QString m_name;
ShoeDescription m_shoe;
};
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"
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;
}
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;
}
Boy::Boy(QObject * parent)
: Person(parent)
{
}
Girl::Girl(QObject * parent)
: Person(parent)
{
}
~~~
最后來看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);
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
qmlRegisterType<ShoeDescription>(); // 注冊該類型,但不進行實例化,這與Person類相同
qmlRegisterType<Person>();
qmlRegisterType<Boy>("People", 1,0, "Boy");
qmlRegisterType<Girl>("People", 1,0, "Girl");
QQmlEngine engine;
QQmlComponent 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!";
if (qobject_cast<Boy *>(party->host()))
qWarning() << "He is inviting:";
else
qWarning() << "She is inviting:";
Person *bestShoe = 0;
for (int ii = 0; ii < party->guestCount(); ++ii) { // 比較每個客人鞋子價格,得出誰穿著最好的鞋
Person *guest = party->guest(ii);
qWarning() << " " << guest->name();
if (!bestShoe || bestShoe->shoe()->price() < guest->shoe()->price())
bestShoe = guest;
}
if (bestShoe)
qWarning() << bestShoe->name() << "is wearing the best shoes!";
} else {
qWarning() << component.errors();
}
return 0;
}
~~~
運行效果:

可以看到,要想實現類似
~~~
shoe { size: 12; color: "white"; brand: "Bikey"; price: 90.0 }
~~~
這種“群屬性”的設置方法,我們只需要設置shoe為只讀就可以了,這樣QML編譯器就不會尋找shoe后面的冒號,進入大括號后的賦值語句,實際上是對ShoeDescription屬性的賦值。
那么如果我們想要實現這種寫法
~~~
shoe: ShoeDescription { size: 12; color: "white"; brand: "Bikey"; price: 90.0 }
~~~
該如何改動呢?
第一點是shoe屬性的聲明,它必須是可讀寫的:
Q_PROPERTY(ShoeDescription *shoe READ shoe WRITE setShoe)?
當然還有對應的setShoe()函數的實現。
第二點是在main.cpp中QML類型的注冊:
qmlRegisterType("People", 1,0, "ShoeDescription");
這里的ShoeDescription就需要被實例化了。
這樣就可以再次運行了。
- 前言
- 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