Extending QML - Default Property Example
扩展QML-默认属性示例
Default Property.
默认属性。
This example builds on:
此示例基于:
- Extending QML - Inheritance and Coercion Example
- Extending QML - Object and List Property Types Example
- Extending QML - Adding Types Example
The Default Property Example is a minor modification of the Extending QML - Inheritance and Coercion Example that simplifies the specification of a BirthdayParty through the use of a default property.
默认属性示例是扩展QML继承和强制示例的一个小修改,该示例通过使用默认属性简化了BirthdayParty的规范。
BirthdayParty {
host: Boy {
name: "Bob Jones"
shoeSize: 12
}
Boy { name: "Leo Hodges" }
Boy { name: "Jack Smith" }
Girl { name: "Anne Brown" }
}
Declaring the BirthdayParty Class
声明BirthdayParty类
The only difference between this example and the last, is the addition of the DefaultProperty
class info annotation.
此示例与上一个示例之间的唯一区别是添加了DefaultProperty类信息注释。
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")
QML_ELEMENT
public:
using QObject::QObject;
Person *host() const;
void setHost(Person *);
QQmlListProperty<Person> guests();
qsizetype guestCount() const;
Person *guest(qsizetype) const;
private:
Person *m_host = nullptr;
QList<Person *> m_guests;
};
The default property specifies the property to assign to whenever an explicit property is not specified, in the case of the BirthdayParty type the guest property. It is purely a syntactic simplification, the behavior is identical to specifying the property by name, but it can add a more natural feel in many situations. The default property must be either an object or list property.
默认属性指定未指定显式属性时要分配给的属性,对于BirthdayParty类型,则指定guest属性。这纯粹是一种语法简化,其行为与按名称指定属性相同,但在许多情况下,它可以增加更自然的感觉。默认属性必须是对象或列表属性。
Running the Example
运行示例
The main.cpp file in the example includes a simple shell application that loads and runs the QML snippet shown at the beginning of this page.
示例中的main.cpp文件包括一个简单的shell应用程序,该应用程序加载并运行本页开头显示的QML代码段。