laugusti
2nd November 2009, 09:47
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:
/**
* List of expanded items.
*/
QModelIndexList m_expandedItemList;
//================================================== ==============
void QResourceTree::addExpandedItem(const QModelIndex& index)
{
QResourceProxyModel * proxyModel = (QResourceProxyModel*)model();
QModelIndex sourceIndex = proxyModel->mapToSource(index);
m_expandedItemList.append(QPersistentModelIndex(so urceIndex));
}
//================================================== ==============
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();
QModelIndex index = proxyModel->mapFromSource(*it);
if (index.isValid() && !isExpanded(index)) {
this->expand(index);
}
}
}
}
}
Code for deletion:
// Is it the right order?
1/ m_root->removeChildren(index.row());
2/ changePersistentIndex(index, QModelIndex());
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
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:
/**
* List of expanded items.
*/
QModelIndexList m_expandedItemList;
//================================================== ==============
void QResourceTree::addExpandedItem(const QModelIndex& index)
{
QResourceProxyModel * proxyModel = (QResourceProxyModel*)model();
QModelIndex sourceIndex = proxyModel->mapToSource(index);
m_expandedItemList.append(QPersistentModelIndex(so urceIndex));
}
//================================================== ==============
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();
QModelIndex index = proxyModel->mapFromSource(*it);
if (index.isValid() && !isExpanded(index)) {
this->expand(index);
}
}
}
}
}
Code for deletion:
// Is it the right order?
1/ m_root->removeChildren(index.row());
2/ changePersistentIndex(index, QModelIndex());
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