Results 1 to 2 of 2

Thread: update treewidget element value

  1. #1
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default update treewidget element value

    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

    Qt Code:
    1. for(int i=0;i<robotvar->getElementsNumber();i++){
    2. QTreeWidgetItem *item = treeWidget->topLevelItem(row);
    3. if(item->childCount() != robotvar->getElementsNumber()){
    4. QTreeWidgetItem *child = new QTreeWidgetItem(item);
    5. child->setText(0,robotvar->getStructureMember(i));
    6. child->setText(1,robotvar->getStructureValue(i,datatype));
    7. }
    8. else{
    9. QTreeWidgetItem *child = item->child(i);
    10. child->setText(1,robotvar->getStructureValue(i,datatype));
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: update treewidget element value

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.