GeƤndert: 5. 1. 2013, 20:46
Persistent crashes
I was trying to delete some items from my model. According to the documentation I tried it with beginRemoveRows() and endRemoveRows(). Well, it didn't work. Crashes everywhere.
The encapsulation of the insert and remove operations with these begin and end functions also enables the model to manage persistent model indexes correctly.
Well, actually it doesn't. After one and a half day I found a way how to do it a way that it doesn't crash anymore. The last hour (well, it was after midnight) I really went mad because I shoot myself in the foot. I started with:
class KGpgRootNode: public KGpgNode { protected: KGpgItemModel *m_model; ... KGpgRootNode::KGpgRootNode(KGpgItemModel *model) : KGpgNode(NULL) { m_model = model; ...
Every item is a child of this root node and now has access to the model. Since there are indirect clients the access was encapsulated in a function. Bad thing: when the root item is destructed the client items can't call upwards to invalidate themselfs. My fault. Next try:
class KGpgNode { protected: KGpgItemModel *m_model; ... KGpgNode::KGpgNode(KGpgNode *parent) { if (parent != NULL) m_model = parent->m_model; else m_model = NULL; ...
Insert an hour of confusion and head-shaking here. Why the hell is m_model always NULL? Of course because I forgot to remove it from KGpgRootNode so it was hiding the member of KGpgNode. After this was found the rest was simple:
KGpgNode::~KGpgNode() { m_model->invalidateIndexes(this); } void KGpgItemModel::invalidateIndexes(KGpgNode *nd) { for (int i = 0; i < persistentIndexList().count(); i++) { QModelIndex idx = persistentIndexList().at(i); KGpgNode *n = nodeForIndex(idx); if (n != nd) continue; changePersistentIndex(idx, QModelIndex()); } }