Results 1 to 2 of 2

Thread: Getting child's ModelIndex while traversing tree

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Getting child's ModelIndex while traversing tree

    So I now have a QTreeWidget established and a signal connected to a slot that processes updateChecked when an item is checked. Problem I’m having now is that I cn’t figure out how to get a QTreeWidget’s ModelIndex as I’m traversing the tree. I would like to used it to compare it to the one that was just cheked.

    There is an indexFromItem() method but it is protected so I can’t use it. Anyone have a suggestion??

    See code below

    Thanks

    Qt Code:
    1. void CtreeCU::updateChecked()
    2. {
    3. // get index to item just checked
    4. QModelIndex idx = twMain->currentIndex();
    5. qDebug("internalId: %ld",idx.internalId());
    6.  
    7. // if item was checked then need to process the rest of the tree
    8. if ( twMain->currentItem()->checkState(0) == Qt::Checked )
    9. {
    10. qDebug("Item is checked");
    11.  
    12. // save current index in tree
    13. currentCheckedIdx = idx;
    14.  
    15. // search for other items that are checked and remove them
    16. processTree();
    17.  
    18. }
    19. else
    20. qDebug("Item is NOT checked");
    21. }
    22.  
    23.  
    24. void CtreeCU::processTree()
    25. {
    26. // loop through all branches and nodes of the tree
    27. for ( int i=0; i< twMain->topLevelItemCount(); i++)
    28. {
    29. QTreeWidgetItem *item = twMain->topLevelItem(i);
    30. processItem(item);
    31. }
    32. }
    33.  
    34.  
    35. void CtreeCU::processItem(QTreeWidgetItem * parent)
    36. {
    37. // process for each child associated with the top node
    38. // this will recurse to lower children on tree
    39. for (int i=0; i< parent->childCount(); i++)
    40. {
    41. QTreeWidgetItem *child = parent->child(i);
    42.  
    43. // ****** PROBLEM AREA *************
    44. QModelIndex idx = twMain->indexFromItem(child);
    45. // qDebug("index of child is: %d ",idx.internalId());
    46. // ************************************
    47.  
    48. // if it is checked that will determine if it is the one to remain
    49. // checked or if it needs its state changed
    50. if ( child->checkState(0) == Qt::Checked )
    51. {
    52. QModelIndex idx = twMain->currentIndex();
    53.  
    54. if ( currentCheckedIdx.internalId() != idx.internalId() )
    55. {
    56. // testing - uncheck selected item
    57. child->setCheckState(0,Qt::Unchecked);
    58. qDebug("internal indexes DID NOT match");
    59.  
    60. }
    61. else
    62. {
    63. // print out sample data
    64. QMap<QString,QVariant> nodeData = twMain->currentItem()->data(0, Qt::UserRole).toMap();
    65. qDebug("method: %s",qPrintable(nodeData.value("method").toString()));
    66. }
    67.  
    68. }
    69. processItem(child);
    70. }
    71. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd October 2007 at 15:13. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting child's ModelIndex while traversing tree

    You either subclass QTreeWidget so you get access to the protected members or change the way you traverse the tree.

    You can use the model and the root QModelIndex to traverse it. See QTreeWidget::model() and QTreeWidget::rootIndex(). It think it can be done.

Similar Threads

  1. traversing tree using mouse clicks
    By krishna.bv in forum Qt Programming
    Replies: 3
    Last Post: 22nd December 2006, 09:15

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.