扩展QML-默认属性示例

Extending QML - Default Property Example

扩展QML-默认属性示例

Default Property.

默认属性。

This example builds on:

此示例基于:

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代码段。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值