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

来源:互联网 发布:少儿趣味编程课程 编辑:程序博客网 时间:2024/06/11 00:22

在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();}
原创粉丝点击