PDA

View Full Version : update treewidget element value



nowire75
6th December 2007, 21:26
hi,

I have this question: I have a two-dimensional array of variable names and variable values. I have make a treewidget with two colums: names and values.
I want update the variables values any second (or more). Same time also the number of variables changed.
I write this code, it work but I things is not the correct way


for(int i=0;i<robotvar->getElementsNumber();i++){
QTreeWidgetItem *item = treeWidget->topLevelItem(row);
if(item->childCount() != robotvar->getElementsNumber()){
QTreeWidgetItem *child = new QTreeWidgetItem(item);
child->setText(0,robotvar->getStructureMember(i));
child->setText(1,robotvar->getStructureValue(i,datatype));
}
else{
QTreeWidgetItem *child = item->child(i);
child->setText(1,robotvar->getStructureValue(i,datatype));
}
}

magland
7th December 2007, 02:58
Here's my advice:

When you want to update the tree, first make two string sets. S1 containing the names of the variables in your tree, and S2 containing the names of the variables in your data. Then go through the tree and delete the nodes that don't appear in S2. Then go through your data, and add a tree node for the variable names that doesn't appear in S1. Then your tree will have the right number of nodes. Finally, go through the tree and update the values.

Might seem like a lot of steps, but I think it's a safe and fast way to do it.

(Use QSet<QString>, and you may also need QHash<QString,int> or something if you need the final step to be fast.)