本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873)
接上文[Qt5官方demo解析集22——Extending QML - Object and List Property Types Example](http://blog.csdn.net/cloud_castle/article/details/37355839)
在上一個例子中,我們為BirthdayParty類創建了帶有一個列表參數的屬性guests,而這個列表參數的類型都是一致的,即Person。然而,使用QML的類型轉換機制,我們可以使這個列表參數的類型變得不同。既然說到了這里,我們先來看看example.qml:
~~~
import People 1.0
// ![0]
BirthdayParty {
host: Boy {
name: "Bob Jones"
shoeSize: 12
}
guests: [ // 要知道注冊屬性使我們需要給其一個固定的參數類型,如int,bool,甚至自定義的類型
Boy { name: "Leo Hodges" }, // 因此想要得到該代碼的效果我們需要使用繼承
Boy { name: "Jack Smith" }, // Boy與Girl均繼承自Person,而我們僅僅將guests注冊為Person就夠了
Girl { name: "Anne Brown" }
]
}
// ![0]
~~~
由于BirthdayParty類的代碼沒有改變,我們就看看Person.h:
~~~
#ifndef PERSON_H
#define PERSON_H
#include <QObject>
class Person : public QObject // Person也沒有改動
{
Q_OBJECT
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;
};
// ![0]
class Boy : public Person // 創建一個Boy繼承自Person
{
Q_OBJECT
public:
Boy(QObject * parent = 0); // 需要一個最基本的構造函數使得QML可以實例化這個對象
};
//! [girl class]
class Girl : public Person // Girl同Boy
{
Q_OBJECT
public:
Girl(QObject * parent = 0);
};
//! [girl class]
// ![0]
#endif // PERSON_H
~~~
Person.cpp:
~~~
#include "person.h"
Person::Person(QObject *parent)
: QObject(parent), m_shoeSize(0)
{
}
QString Person::name() const
{
return m_name;
}
void Person::setName(const QString &n)
{
m_name = n;
}
int Person::shoeSize() const
{
return m_shoeSize;
}
void Person::setShoeSize(int s)
{
m_shoeSize = s;
}
// ![1]
Boy::Boy(QObject * parent) // 由于該例子只是簡單演示繼承,因此也并未為派生類添加額外的功能
: Person(parent)
{
}
Girl::Girl(QObject * parent) // 構造函數是必須的
: Person(parent)
{
}
// ![1]
~~~
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");
//![0]
qmlRegisterType<Person>(); // 我們依然需要注冊Person,否則Boy與Girl無法被強制轉換為Person
//![0] // 該函數參數為空,因為我們并不需要實例化一個Person的對象
//![register boy girl]
qmlRegisterType<Boy>("People", 1,0, "Boy"); // 注冊Boy和Girl
qmlRegisterType<Girl>("People", 1,0, "Girl");
//![register boy 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:";
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