Unsolved QTreeView item expansion
-
Hey guys. Basically I have a QTreeView, a QSortFilterProxyModel and an implementation of a QAbstractItemModel. The model is simple, there is a root item, then at most two further levels of items.
For example lets say my model represents Films and their actors such that the tree might look like this:
Lord of the rings Sean Bean Ian Holm Ian McKellen Elijah Wood ... etc ... Watchmen Billy Crudup Patrick Wilson Jeffrey Dean Morgan Matthew Goode ... etc ... ... etc ...
Sometimes the model is fully rebuilt, other times times there is a user filter that can be applied which may hide or show films. I keep track of which films are expanded so when an item comes into view or the model is rebuilt then I can expand them if required.
Now this is something I have done before with no issue, but in this particular instance for some reason expanding the nodes individually using QTreeView::setExpanded(QModelIndex, bool) does not work. the QTreeView::expandAll() function works though so I am not sure what is happening.
I have tried the following code snippet as a test to see if I can programmatically expand all nodes individually but it does not work, even if defered using a timer by something stupid like 5 seconds (also note at this point I have confirmed that the QModelIndex points to the expected item by checking the internal data).
for( int i = 0; i < mProxyModel->sourceModel()->rowCount(); i++ ) { QModelIndex sourceIndex = mProxyModel->sourceModel()->index( i, 0 ); QModelIndex mappedIndex = mProxyModel->mapFromSource( sourceIndex ); if( !mappedIndex.isValid() ) continue; mTreeView->setExpanded( mappedIndex, true ); }
Using the following snippet does work however:
mTreeView->expandAll();
Does anyone know of anything that could be causing this?! Or perhaps some advice on how to debug this further?
Kind regards, Aaron.
-
@aatwo
when is your expanding-loop called? -
@raven-worx Under normal conditions right after my model emits a signal to indicate that it has been rebuilt, and also just after the user modifies the filter text:
void onFilterTextChanged(const QString& text) { mProxyModel->setFilterText(text); // This function stores the filter text as a member and then calls 'QSortFilterProxyModel::invalidateFilter()' for( int i = 0; i < mProxyModel->sourceModel()->rowCount(); i++ ) { QModelIndex sourceIndex = mProxyModel->sourceModel()->index( i, 0 ); QModelIndex mappedIndex = mProxyModel->mapFromSource( sourceIndex ); if( !mappedIndex.isValid() ) continue; mTreeView->setExpanded( mappedIndex, true ); } }
But even if I take that loop, and put it in a slot that gets called after 10 seconds it doesn't work. It's worth noting that my application has another similar model + proxy + view implementation, and I tried adding a slot that gets called after 10 seconds to expand all the tree items using the exact same loop as above and it works as expected. The proxy models for these two models are almost identical so my only guess at this point is that there is something odd happening in the model.
-
@aatwo Did you receive a solution?