對從c++中向qml中匯入list的操作的深入學習
阿新 • • 發佈:2019-01-09
在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();
}