PDA

View Full Version : List all elements of a QTreeWidget from top to bottom



ricardosf
11th June 2010, 23:58
I'm having dificulties getting all the values of a N levels QTreeWidget by the folloying order:

For example:

A1
B1
B2
B3
C1
C2
D1
D2
C3

and i want to put them in an array like: A1 B1 B2 B3 C1 C2 D1 D2 C3

I'm having big problems trying to find this algorithm. For a finit levels tree i can do it but for N its more dificult.

Someone can help me with this?

Maybe i can use the model for that?

wysota
12th June 2010, 02:26
I'm not sure if I follow what you mean by the letters and numbers but isn't that a simple case of descending node traversal using a recursive call? First "visit" yourself, then visit each of your children in order by calling the same visiting function on them.

Something like:

void visitTree(QStringList &list, QTreeWidgetItem *item){
list << item->text(0);
for(int i=0;i<item->childCount(); ++i)
visitTree(list, item->child(i));
}

QStringList visitTree(QTreeWidget *tree) {
QStringList list;
for(int i=0;i<tree->topLevelItemCount();++i)
visitTree(list, tree->topLevelItem(i));
return list;
}
//...
QStringList result = visitTree(myTree);

ricardosf
12th June 2010, 02:46
Thanks a lot. It solved my problem :) :)