Results 1 to 3 of 3

Thread: How can I traverse all of the items in a QStandardItemModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I traverse all of the items in a QStandardItemModel

    Pseudo code:
    Qt Code:
    1. for i = rowCount() - 1 to 0:
    2. push index( i, 0 )
    3. while stack not empty:
    4. pop idx from stack
    5. process idx
    6. if hasChildren( idx ):
    7. for i = rowCount( idx ) - 1 to 0:
    8. push index( i, 0, idx )
    To copy to clipboard, switch view to plain text mode 
    (this code assumes that you keep items as rows and columns just represent their properties)

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I traverse all of the items in a QStandardItemModel

    Ok i found a way to build a dom tree .. from a QStandardItemModel

    a static function to retry child item

    Qt Code:
    1. static QList<QStandardItem*> childList( QStandardItem *qi )
    2. {
    3. QStandardItemModel *pm = qi->model();
    4. QList<QStandardItem*> rList;
    5. QModelIndex in = qi->index();
    6. if (!in.isValid()) {
    7. return rList;
    8. }
    9. if (qi->rowCount() == 0) {
    10. return rList;
    11. }
    12. for (int e = 0; e < qi->rowCount(); ++e) {
    13. QModelIndex si = pm->index(e,qi->column(),in);
    14. QStandardItem *iz = pm->itemFromIndex(si);
    15. if (iz) {
    16. rList << iz;
    17. }
    18. }
    19. return rList;
    20. }
    To copy to clipboard, switch view to plain text mode 


    and a class to build a xml

    Qt Code:
    1. void BookTree::subIterate()
    2. {
    3. QStandardItemModel *pm = qobject_cast<QStandardItemModel *>(model());
    4. if (!pm) {
    5. return;
    6. }
    7. int rw = 0;
    8. int cw = 0;
    9. int ctot = 0;
    10. const int cools = pm->columnCount();
    11. const int rows = pm->rowCount();
    12. qDebug() << "-- iter ---------------------------------------------------------";
    13. for (int i = 0; i < cools; ++i) {
    14. const QString htxt = pm->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString();
    15. const QString htxt1 = pm->headerData(i,Qt::Vertical,Qt::DisplayRole).toString();
    16. line << qMax(htxt,htxt1);
    17. }
    18. QList<QStandardItem*> list;
    19. for (int e = 0; e < rows; ++e) {
    20. QStandardItem *ix_1 = pm->item(e,0);
    21. QStandardItem *ix_2 = pm->item(e,1);
    22. list.clear();
    23. list = childList(ix_1);
    24. ctot = list.size();
    25. int level = 0;
    26. qDebug() << "# lev." << level <<" line " << e << " txt " << ix_1->text() << " child " << ctot;
    27. if (ctot !=0) {
    28.  
    29. level++;
    30. foreach (QStandardItem *ix,list) {
    31. list.clear();
    32. ctot = 0;
    33. list = childList(ix);
    34. ctot = list.size();
    35. qDebug() << "# lev." << level <<" line " << e << " txt " << ix->text() << " child " << ctot;
    36.  
    37. }
    38. }
    39. }
    40. qDebug() << "-- iter ---------------------------------------------------------";
    41. }
    To copy to clipboard, switch view to plain text mode 

    now i must only transform the tree model -> flat like table to validate link...

  3. The following user says thank you to patrik08 for this useful post:

    neuronet (17th February 2015)

Similar Threads

  1. Light items for the graphicsView
    By maverick_pol in forum Qt Programming
    Replies: 12
    Last Post: 1st November 2007, 18:51
  2. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 12:20

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.