本系列所有文章可以在这里查看[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