Qt5官方demo解析集24——Extending QML - Default Property Example
2014-07-06 22:13
465 查看
本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873
接上文Qt5官方demo解析集23——Extending
QML - Inheritance and Coercion Example
有时我们看到某个QML类型的声明中,某些数据并没有放在属性 + :后面,它们实际上属于这个类型的默认属性。而这个demo则向我们介绍了这个技术。
这个例子与上一篇几乎没有什么改动,除了两个小地方。
第一处在QML描述文件中example.qml:
另一个地方则是我们BirthdayParty类的声明,BirthdayParty.h:
接上文Qt5官方demo解析集23——Extending
QML - Inheritance and Coercion Example
有时我们看到某个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
相关文章推荐
- Qt5官方demo解析集22——Extending QML - Object and List Property Types Example
- Qt5官方demo解析集23——Extending QML - Inheritance and Coercion Example
- Qt5官方demo解析集29——Extending QML - Property Value Source Example
- Qt5官方demo解析集27——Extending QML - Attached Properties Example
- Qt5官方demo解析集28——Extending QML - Signal Support Example
- Qt5官方demo解析集21——Extending QML - Adding Types Example
- Qt5官方demo分析集29——Extending QML - Property Value Source Example
- Qt5官方demo解析集21——Extending QML - Adding Types Example
- Qt5官方demo解析集25——Extending QML - Methods Example
- Qt5官方demo解析集8——Shaped Clock Example
- Qt5官方demo解析集7——Analog Clock Example
- Qt5官方demo解析集30——Extending QML - Binding Example
- 关于Qt5官方demo解析集36——Wiggly Example
- Qt5官方demo解析集16——Chapter 2: Connecting to C++ Methods and Signals
- Qt5官方demo解析集26——Extending QML - Grouped Properties Example
- Qt5官方demo解析集(36个)
- Qt5官方demo解析集9——Analog Clock Window Example
- Qt5官方demo解析集6——Loopback Example
- Qt5官方demo解析集17——Chapter 3: Adding Property Bindings
- Qt5官方Demo解析集5——Threaded Fortune Server