Typically one traverses a tree-like structure using a recursive function:
{
if ( pItem == 0 )
return;
// Add code here to save the text in the item's columns
// Hint: use pItem->columnCount() to get the number of columns for the current item.
// Iterate over the columns.
// Write each column's text to the same row in the CSV file, separated by commas. Use the "indent" value to insert empty CSV fields
// at the start of the row so you can preserve the tree structure. Or choose some other way to indicate
// in the CSV file what the current level is (like maybe just write the indent value as the first
// field in the CSV row)
// Now save children
int nKids = pItem->childCount();
for ( int nKid = 0; nKid < nKids; ++nKid )
saveTreeNode( pItem->child( nKid ), indent + 1 );
}
void saveTreeNode( QTreeWidgetItem * pItem, int indent )
{
if ( pItem == 0 )
return;
// Add code here to save the text in the item's columns
// Hint: use pItem->columnCount() to get the number of columns for the current item.
// Iterate over the columns.
// Write each column's text to the same row in the CSV file, separated by commas. Use the "indent" value to insert empty CSV fields
// at the start of the row so you can preserve the tree structure. Or choose some other way to indicate
// in the CSV file what the current level is (like maybe just write the indent value as the first
// field in the CSV row)
// Now save children
int nKids = pItem->childCount();
for ( int nKid = 0; nKid < nKids; ++nKid )
saveTreeNode( pItem->child( nKid ), indent + 1 );
}
To copy to clipboard, switch view to plain text mode
You start the whole thing off with a call to:
saveTreeNode( pTree->invisibleRootItem(), 0 );
saveTreeNode( pTree->invisibleRootItem(), 0 );
To copy to clipboard, switch view to plain text mode
Bookmarks