Results 1 to 3 of 3

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

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

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

    I need a model tree to save PDF Bookmark like QStandardItemModel
    and this model must combine on anchorNames () on a QTextDocument

    i need a model to validate anchorNames () at QTextDocument .....

    at end i save the tree on this way:
    Qt Code:
    1. <fo:bookmark-tree>
    2. <fo:bookmark internal-destination="sec0" >
    3. <fo:bookmark-title>Internal dest.0</fo:bookmark-title>
    4. <fo:bookmark internal-destination="sec1" >
    5. <fo:bookmark-title>Internal dest.1</fo:bookmark-title>
    6. </fo:bookmark>
    7. <fo:bookmark internal-destination="sec2" >
    8. <fo:bookmark-title>Internal dest.2</fo:bookmark-title>
    9. </fo:bookmark>
    10. </fo:bookmark>
    11. </fo:bookmark-tree>
    To copy to clipboard, switch view to plain text mode 

    I search a way to traverse or iterate all node from model
    i find only this here ...

    http://troll.no/developer/knowledgeb...ndarditemmodel

    Is here other way?


    on read xml-bookmark i make it so...

    Qt Code:
    1. void BookMarkModelRead::openRootBookmark( const QDomElement e )
    2. {
    3. if (e.tagName() !="fo:bookmark-tree")
    4. {
    5. return;
    6. }
    7. foundTree = true;
    8. model = new QStandardItemModel();
    9. internalLinkFound.clear();
    10. setHeader();
    11. //////////////qDebug() << "### openRootBookmark -> " << e.tagName();
    12. QDomElement child = e.firstChildElement("fo:bookmark");
    13. while (!child.isNull()) {
    14. model->invisibleRootItem()->appendRow(Compose(child,0));
    15. child = child.nextSiblingElement("fo:bookmark");
    16. }
    17. }
    18.  
    19. QList<QStandardItem *> BookMarkModelRead::Compose( const QDomElement e , const int leveldeep )
    20. {
    21. Q_ASSERT ( e.tagName() == "fo:bookmark" );
    22. treeLoop++;
    23. QList<QStandardItem *> diritto;
    24. QIcon icob = createBookColorIcon( Qt::darkRed );
    25. QString txt = e.firstChildElement("fo:bookmark-title").text();
    26. if (txt.size() < 2) {
    27. txt = tr("No Title found!");
    28. icob = createBookColorIcon( Qt::red );
    29. }
    30. const QString link = e.attribute("internal-destination","null");
    31. internalLinkFound.append(link);
    32. qDebug() << "### read -> " << txt << "-" << treeLoop;
    33.  
    34. QStandardItem *item0 = new QStandardItem(txt);
    35. item0->setData(leveldeep,Qt::UserRole);
    36. item0->setData(bold_base_font,Qt::FontRole);
    37. item0->setIcon ( icob );
    38. item0->setFlags( flags );
    39.  
    40. diritto.append(item0);
    41.  
    42. QStandardItem *item1 = new QStandardItem(link);
    43. item1->setFlags( flags );
    44. item1->setData(leveldeep,Qt::UserRole);
    45.  
    46. diritto.append(item1);
    47.  
    48. QStandardItem *item2 = new QStandardItem(QString("%1").arg(leveldeep));
    49. item2->setFlags( Qt::ItemIsEnabled );
    50. item2->setData(leveldeep,Qt::UserRole);
    51.  
    52. diritto.append(item2);
    53.  
    54. if (!e.firstChildElement("fo:bookmark").isNull() && leveldeep == 0 ) {
    55. /* one level deep down child */
    56. QDomElement child = e.firstChildElement("fo:bookmark");
    57. while (!child.isNull()) {
    58. if ( child.tagName() == "fo:bookmark") {
    59. const QString nextlink = child.attribute("internal-destination","null");
    60. if (!internalLinkFound.contains(nextlink)) {
    61. diritto.first()->appendRow(Compose(child,leveldeep + 1));
    62. }
    63. }
    64.  
    65. child = child.nextSiblingElement();
    66. }
    67. } else if (!e.nextSiblingElement("fo:bookmark").isNull() && leveldeep > 0 ) {
    68. /* same level is only next on deep */
    69.  
    70. }
    71. return diritto;
    72. }
    To copy to clipboard, switch view to plain text mode 

    and to read the model it is not possibel?
    i can not use QTreeWidget / QTreeWidgetItem why ... i can not set a model at him...
    only QTreeView can set a model .... i know only a way to use sqlite3 as model...
    Must i use sqlite3 to traverse all tree? QStandardItemModel can not make this job..

    the resut... is not possible to read back from QTreeView ...!! onewayQTreeView :-(


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

  3. #3
    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...

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