对从c++中向qml中导入list的操作的深入学习

在qml中对数组Array可以进行push [index] .length clear;等操作
而从c++中向qml中导入list

//这里是实例化一个QQmlListProperty,分别使用这四个函数指针
QQmlListProperty<Person> BirthdayParty::guests()
{
    return QQmlListProperty<Person>(this, this,
             &BirthdayParty::appendGuest,
             &BirthdayParty::guestCount,
             &BirthdayParty::guest,
             &BirthdayParty::clearGuests);
//    return QQmlListProperty<Person>(this, m_guests);
}

//这是最终执行的函数
void BirthdayParty::appendGuest(Person* p) {
    qDebug() << "appendGuest1";
    m_guests.append(p);
}
int BirthdayParty::guestCount() const
{
    return m_guests.count();
}
Person *BirthdayParty::guest(int index) const
{
    return m_guests.at(index);
}
void BirthdayParty::clearGuests() {
    qDebug() << "lll";
    while(m_guests.size() > 0)
    {
        m_guests.takeFirst()->destroyed();
    }
//    return m_guests.clear();
}

// ![0]

//这三个函数是在qml中分别操作"push" "=[]"  "[index]"  ".length"时使用的
void BirthdayParty::appendGuest(QQmlListProperty<Person>* list, Person* p) {
    qDebug() << "appendGuest2";
    reinterpret_cast< BirthdayParty* >(list->data)->appendGuest(p);
}

void BirthdayParty::clearGuests(QQmlListProperty<Person>* list) {
    qDebug() << "clearGuests";
    reinterpret_cast< BirthdayParty* >(list->data)->clearGuests();
}

Person* BirthdayParty::guest(QQmlListProperty<Person>* list, int i) {
    qDebug() << "guest";
    return reinterpret_cast< BirthdayParty* >(list->data)->guest(i);
}

int BirthdayParty::guestCount(QQmlListProperty<Person>* list) {
    qDebug() << "guestCount";
    return reinterpret_cast< BirthdayParty* >(list->data)->guestCount();
}

猜你喜欢

转载自blog.csdn.net/wei375653972/article/details/78007795