本系列所有文章可以在這裡檢視http://blog.csdn.net/cloud_castle/article/category/2123873
接上文Qt5官方demo解析集25——Extending QML - Methods Example
如果之前看過了我前面介紹粒子系統的朋友,應該對
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<ShoeDescription>("People", 1,0, "ShoeDescription");
這裡的ShoeDescription就需要被執行個體化了。
這樣就可以再次運作了。