I have a QStandardItemModel that I am using in a QTreeView. It has several levels, and I want to allow the user to expand/contract a specific level. Some of the levels are siblings, I want to allow the user to expand one sibling, but not another. I store the 'name' of the level in Qt::UserRole+1. When the QTreeView displays, a QComboBox with the names gets populated, along with defaults of Expand All and Expand None. I have a signal/slot triggered by changing the Expand Level in the combo box. Expand All and Expand None work fine. I am using the code below:
void RWPreview
::changeExpand( QString level
) {
QModelIndexList idx_list;
if( level.compare( "All") == 0 )
rpt_view-> expandAll();
else if( level.compare("None") == 0 )
rpt_view-> collapseAll();
else
{
idx_list = rpt_view->model()->match(st_idx, Qt::UserRole+1, qVariantFromValue(level), -1, Qt::MatchRecursive );
QListIterator<QModelIndex> i( idx_list );
while (i.hasNext())
rpt_view->expand( i.next() );
}
}
void RWPreview::changeExpand( QString level )
{
QModelIndexList idx_list;
if( level.compare( "All") == 0 )
rpt_view-> expandAll();
else if( level.compare("None") == 0 )
rpt_view-> collapseAll();
else
{
QModelIndex st_idx = ((QStandardItemModel*)rpt_view->model() )->index(0,0);
idx_list = rpt_view->model()->match(st_idx, Qt::UserRole+1, qVariantFromValue(level), -1, Qt::MatchRecursive );
QListIterator<QModelIndex> i( idx_list );
while (i.hasNext())
rpt_view->expand( i.next() );
}
}
To copy to clipboard, switch view to plain text mode
I get the expected number of model indexes for the expected rows in the QModelIndexList. It completes the while loop, and exits the if statement. However, when it exits the function, I get an exception. Does anyone have an idea what I can try? If I dont' create the QModelIndexList, it works fine (but obviously, doesn't expand nodes). I'm puzzled!
Bookmarks