PDA

View Full Version : how to save a treewidget



namitha
9th August 2016, 13:30
Hi,
Can anyone help on saving a TreeWidget into .csv file. I tried different ways but i am able to save only parent item, i am not able to traverse through children.
code is given in attachment.
Thanks in advance
namitha

ChrisW67
9th August 2016, 14:16
Firstly.... A screen shot of code? Copy and paste is not only easier but more useful.

Secondly, your code makes no attempt to descend into children records if they exist.

d_stranz
10th August 2016, 05:12
Typically one traverses a tree-like structure using a recursive function:


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 );
}


You start the whole thing off with a call to:



saveTreeNode( pTree->invisibleRootItem(), 0 );

namitha
17th September 2016, 08:06
Hi,
I tried but only root is saved everytime. Can you please help me with the code that

How to 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).

anda_skoa
17th September 2016, 09:53
I tried but only root is saved everytime. Can you please help me with the code that

If you post the code that does not work we can likely help you find the problem in it.

Cheers,
_