Results 1 to 6 of 6

Thread: Traversing a QTreeWidget

  1. #1
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Traversing a QTreeWidget

    From a previous post, I was able to align my Model with the View sucessfully so that colors of leaves in a QTreeWidget can be changed.

    I cannot seem to figure out how to use the QTreeWidgetItem to traverse all of the tree's childless leaves. I wish to change the color of these leaves based on a flag setting. Any suggestions on how to use the iterator or another method?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing a QTreeWidget

    So are you using a view or a widget?

    To iterate QTreeWidget child items use QTreeWidget::topLevelItem() to access top level items and then recursively use QTreeWidgetItem::child() to iterate over lower-level children and their children.

  3. #3
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Traversing a QTreeWidget

    I can do this recursively, but I wanted to do it iteratively. This way, I can have explicit access to different levels of the tree... This code should be easy to read for the next guy that will pick it up. This is the solution I have thus far, but it has some holes:

    Qt Code:
    1. void DatabaseTreeWidget::Update_Tree_From_Flags()
    2. {
    3. // Iterate through patients...
    4. int Patients = model()->rowCount();
    5. for(int i=0; i<Patients; ++i)
    6. {
    7. QModelIndex Patient_Node = model()->index(i,0);
    8. model()->setData(Patient_Node, Qt::blue, Qt::TextColorRole);
    9.  
    10. //Iterate through study...
    11. //int Study = QAbstractItemModel::rowCount(model()->index(i,0,Patient_Node));
    12. for(int j=0; j<Study; ++j)
    13. {
    14. QModelIndex Study_Node = model()->index(j,0,Patient_Node);
    15. model()->setData(Study_Node, Qt::green, Qt::TextColorRole);
    16.  
    17. //Iterate through series...
    18. //int Series = model()->index(i,0,Patient_Node).rowCount();
    19. for(int k=0; k<Series; ++k)
    20. {
    21. QModelIndex Series_Node = model()->index(k,0,Study_Node);
    22. model()->setData(Series_Node, Qt::red, Qt::TextColorRole);
    23. }
    24.  
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    This code works when I put in constants for Patients, Study, and Series. The question is, how do I determine this programmatically? I need something like rowCount, but for QModelIndex.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing a QTreeWidget

    You can check if the index is valid before you access it.

    Qt Code:
    1. for(QModelIndex Series_Node = model()->index(0,0,Study_Node);
    2. Series_Node.isValid();
    3. Series_Node = model()->index(Series_Node().row()+1, 0, Study_Node)
    4. ){
    5. model()->setData(Series_Node, Qt::red, Qt::TextColorRole);
    6. }
    To copy to clipboard, switch view to plain text mode 

    BTW. What advantage does it have over recursive access? Because I don't see it... maybe looking too much at Interview code made be a little blind

  5. #5
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Traversing a QTreeWidget

    Not much of a functional difference between recursive and iterative approaches. Where iterative use some sort of storage variable and increment to keep track of its progress, recursion uses the call stack to keep track of its progress. Some solutions to some problems are solved simpler recursively than iteratively. As for my purposes, I just want something readable since recursion is very short, terse code that is difficult to trace at times. Iterative approaches are clearer to the eye.

    Theoretically, for every recursive solution to a problem, there is a recursive one to match. The matched pair may have great discrepencies in how they use memory and their codespace.

    I tried to modify your code so that I would first be able to change the color of the top level items. This is what I tried:

    Qt Code:
    1. // Iterate through patients...
    2. for(QModelIndex Patient_Node = model()->index(0,0);
    3. Patient_Node.isValid();
    4. Patient_Node = model()->index(Patient_Node.row()+1,0) )
    5. {
    6. model()->setData(Patient_Node, Qt::red, Qt::TextColorRole);
    7. }
    To copy to clipboard, switch view to plain text mode 

    When executing, I get:

    ASSERT failure in QList<T>::at: "index out of range", file /export/data/jsalik/local/Qt-4.1.1/include/QtCore/qlist.h, line 361


    indicating that something out of range was being indexed.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Traversing a QTreeWidget

    If you use your own model, it might indicate that there is something wrong with its index() method.

Similar Threads

  1. how to sync a QTreeWidget and a QListWidget?
    By zl2k in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2008, 20:55
  2. QTreeWidget Drag and Drop
    By tommydent in forum Qt Programming
    Replies: 10
    Last Post: 25th August 2008, 15:25
  3. Having trouble clearing a QTreeWidget.
    By Nyphel in forum Qt Programming
    Replies: 28
    Last Post: 10th October 2007, 15:33
  4. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.