QPersistentModelIndex problem
Hi all,
Here is my problem ... I have a widget containing 4 tree views connected to the same model. I store QModelIndex to be able to restore expanded items when I switch from a tree view to another.
In order to do it, I want to use QPersistentModelIndex because if I delete a QModelIndex from my model, the QPersistentModelIndex would become invalid. At least, it's what I understood from the documentation.
Here is my code:
Code:
/**
* List of expanded items.
*/
QModelIndexList m_expandedItemList;
//================================================================
void QResourceTree::addExpandedItem(const QModelIndex& index)
{
QResourceProxyModel * proxyModel = (QResourceProxyModel*)model();
QModelIndex sourceIndex
= proxyModel
->mapToSource
(index
);
}
//================================================================
void QResourceTree::restoreExpandedItems() {
if (!m_expandedItemList.empty()) {
/* Restore expanded items */
QModelIndexList::iterator it;
for (it=m_expandedItemList.begin() ; it != m_expandedItemList.end(); it++) {
if ((*it).isValid()) {
QResourceProxyModel * proxyModel = (QResourceProxyModel*)model();
if (index.isValid() && !isExpanded(index)) {
this->expand(index);
}
}
}
}
}
Code for deletion:
Code:
// Is it the right order?
1/ m_root->removeChildren(index.row());
Anytime I delete an expanded item, when I restore the expanded item list, it crashes because my QPersistentModelIndex is still valid.
How do we get a persistent index from a model index? I thought it would be by using the constructor, maybe I am wrong?
Anybody has an example showing how to use QPersistentModelIndex?
Thanks,
Lionel
Re: QPersistentModelIndex problem
you must not store the perstistent model indexes in a QModelIndexList; use
Code:
QList<QPersistentModelIndex>
instead
HTH
Re: QPersistentModelIndex problem
Thank you Caduel,
It works perfectly now!
Regards,
Lionel