扩展QML-对象和列表属性类型示例

Extending QML - Object and List Property Types Example

扩展QML-对象和列表属性类型示例

Exporting C++ Properties.

导出C++属性。

This example builds on:

此示例基于:

The Object and List Property Types example shows how to add object and list properties in QML. This example adds a BirthdayParty type that specifies a birthday party, consisting of a celebrant and a list of guests. People are specified using the People QML type built in the previous example.

对象和列表属性类型示例显示了如何在QML中添加对象和列表属性。此示例添加了指定生日派对的生日派对类型,生日派对由庆祝者和来宾列表组成。使用上一示例中构建的人员QML类型指定人员。

BirthdayParty {
    host: Person {
        name: "Bob Jones"
        shoeSize: 12
    }
    guests: [
        Person { name: "Leo Hodges" },
        Person { name: "Jack Smith" },
        Person { name: "Anne Brown" }
    ]
}

Declare the BirthdayParty

宣布生日聚会

The BirthdayParty class is declared like this:

BirthdayParty类声明如下:

class BirthdayParty : public QObject
{
    Q_OBJECT
    Q_PROPERTY(Person *host READ host WRITE setHost)
    Q_PROPERTY(QQmlListProperty<Person> guests READ guests)
    QML_ELEMENT
public:
    using QObject::QObject;

    Person *host() const;
    void setHost(Person *);

    QQmlListProperty<Person> guests();
    void appendGuest(Person *);
    qsizetype guestCount() const;
    Person *guest(qsizetype) const;
    void clearGuests();
    void replaceGuest(qsizetype, Person *);
    void removeLastGuest();

private:
    static void appendGuest(QQmlListProperty<Person> *, Person *);
    static qsizetype guestCount(QQmlListProperty<Person> *);
    static Person* guest(QQmlListProperty<Person> *, qsizetype);
    static void clearGuests(QQmlListProperty<Person> *);
    static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *);
    static void removeLastGuest(QQmlListProperty<Person> *);

    Person *m_host = nullptr;
    QList<Person *> m_guests;
};

The class contains a member to store the celebrant object, and also a QList<Person *> member.

​该类包含一个存储celebrant对象的成员,以及一个QList成员。

In QML, the type of a list properties - and the guests property is a list of people - are all of type QQmlListProperty<T>. QQmlListProperty is simple value type that contains a set of function pointers. QML calls these function pointers whenever it needs to read from, write to or otherwise interact with the list. In addition to concrete lists like the people list used in this example, the use of QQmlListProperty allows for "virtual lists" and other advanced scenarios.

​在QML中,列表属性的类型-来宾属性是人的列表-都是QQmlListProperty类型。QQmlListProperty是一种简单的值类型,它包含一组函数指针。QML在需要读取、写入列表或与列表交互时调用这些函数指针。除了像本例中使用的人员列表这样的具体列表之外,QQmlListProperty的使用还允许“虚拟列表”和其他高级场景。

Define the BirthdayParty

定义BirthdayParty

The implementation of BirthdayParty property accessors is straight forward.

BirthdayParty属性访问器的实现非常简单。

Person *BirthdayParty::host() const
{
    return m_host;
}

void BirthdayParty::setHost(Person *c)
{
    m_host = c;
}

QQmlListProperty<Person> BirthdayParty::guests()
{
    return {this, this,
             &BirthdayParty::appendGuest,
             &BirthdayParty::guestCount,
             &BirthdayParty::guest,
             &BirthdayParty::clearGuests,
             &BirthdayParty::replaceGuest,
             &BirthdayParty::removeLastGuest};
}

void BirthdayParty::appendGuest(Person *p)
{
    m_guests.append(p);
}

qsizetype BirthdayParty::guestCount() const
{
    return m_guests.count();
}

Person *BirthdayParty::guest(qsizetype index) const
{
    return m_guests.at(index);
}

void BirthdayParty::clearGuests() {
    m_guests.clear();
}

void BirthdayParty::replaceGuest(qsizetype index, Person *p)
{
    m_guests[index] = p;
}

void BirthdayParty::removeLastGuest()
{
    m_guests.removeLast();
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值