PDA

View Full Version : QTreeWidget - Parsing Data.



Preeteesh
17th June 2007, 11:32
Hi,

I have a QTreeWidget. I need to parse data from it and write it to a file. Can some one tell me how to read QTreeWidgetItems from QTreeWidget.

Please post some code if possible.

Thanks..

marcel
17th June 2007, 11:47
One way is to create a recursive function that traverses the tree.
I assume you only want to save the data in the leaf nodes.


void SomeClass::traverseTree(QTreeWidgetItem root)
{
if( root && root->childCount())
{
int childCount = root->childCount();
for( int i = 0; i != childCount(); i++ )
traverseTree( root->child(i);
//Assuming you have any extra data in items that are not terminal nodes,
//then you can save it here.
}
else if( !root->childCount() ) //this means it is a leaf
{
//save whatever data you have in your item in the file.
}
}


This should pretty much work.

Regards

Preeteesh
17th June 2007, 12:50
Hi,

Thats seems good but how to get root node that i will pass to this function. Also i need to save Root node data as well as leaf node in file. Below is sample.

*****RootNode1*****
Leaf Node1
Leaf Node2
*****RootNode2*****
Leaf Node1
Leaf Node2

Thanks...

marcel
17th June 2007, 12:57
You can obtain it with QTreeWidget::invisibleRootItem.
You must call the method I've given you only once, with this item as parameter.

Regards