PDA

View Full Version : Problem in own model (modeltest fail)



airbites
30th July 2009, 10:29
Hello,

I'm testing my model with qt modeltest but two tests fail: rowsInserted and rowsRemoved.

This is the method to delete an item:

void UserModel::delItem(const QString &nick)
{
int row = findItem(nick);
qDebug() << "Going to remove " << nick;
if(row < 0) {
qDebug() << "No item found with nick " << nick;
return;
}
UserItem *u = userList[row];
beginRemoveRows(QModelIndex(), row, row);
qDebug() << "Item " << u->nick << " removed";
userList.removeAt(row);
delete u;
qDebug() << "Really removed? " << (findUser(nick) < 0 ? "Yes" : "No!");
endRemoveRows();
}

I have added more debug to modeltest.cpp and here the result:



Going to remove "foobar"
User "foobar" removed
Really removed? Yes
rowsRemoved start= 41 end= 41 oldsize= 192 parent= "" current rowcount= 192
isIndexValid No
itemWasRemoved: QVariant(, )
ASSERT: "c.oldSize - (end - start + 1) == model->rowCount(parent)" in file ./modeltest/modeltest.cpp, line 537
The item is really removed from userList (QList<UserItem *>) but the value returned by rowCount() doesn't reflect the change. rowCount() implementaion is ok:

int UserModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : userList.count();
}

Any idea? Thx

yogeshgokul
30th July 2009, 10:34
After deletion, update the view. Better use:

bool QAbstractItemModel::removeRow ( int row, const QModelIndex & parent = QModelIndex() )

wysota
30th July 2009, 10:53
rowsInserted and rowsRemoved will fail if you didn't reimplement appropriate methods from the model..

airbites
30th July 2009, 11:06
So your suggestion is to implement removeRows and insertRow instead of own methods?