Results 1 to 3 of 3

Thread: Write tree model to file

  1. #1
    Join Date
    Jun 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Write tree model to file

    I'm trying to come up with a way to write a tree model to a file. My tree model has child items that can have child items and so on. The problem is each parent item doesn't know the depth of its "branch" of the tree which makes iterating through the model difficult. You'd think that you could just check if an item has children and write those, but then you would have to account for child items having child items leaving you with an infinite amount of for statements. Anyways, is there some sort of method for writing these kind of data models to a file using QDataStream?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Write tree model to file

    Can you say "recursion"?

    Qt Code:
    1. void WriteIndex( const QAbstractItemModel & model, const QModelIndex & index, QTextStream & stream, int level )
    2. {
    3. stream << level << index.row() << index.column() << index.data() << endl( stream );
    4. if ( model.hasChildren( index ) )
    5. {
    6. int nRows = model.rowCount( index );
    7. int nCols = model.columnCount( index );
    8. for ( int nRow = 0; nRow < nRows; ++nRow )
    9. {
    10. for ( int nCol = 0; nCol < nCols; ++nCol )
    11. {
    12. WriteIndex( model, model.index( nRow, nCol, index ), stream, level + 1 );
    13. }
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    and you start the whole thing off with the model index that is the root of your tree and level = 0.

    Since the QModelIndex::data() method returns a QVariant, you'll probably need to call one of the QVariant::to...() methods when writing.
    Last edited by d_stranz; 16th May 2012 at 03:58.

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

    hill_rg (7th April 2013)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Write tree model to file

    This is a classic recursive algorithm application.

    Edit: Must remember to preview before submitting... especially when I've been distracted for a while.

  5. The following user says thank you to ChrisW67 for this useful post:

    d_stranz (16th May 2012)

Similar Threads

  1. SQL Tree Model Help
    By MTK358 in forum Newbie
    Replies: 9
    Last Post: 22nd June 2015, 15:02
  2. Replies: 2
    Last Post: 2nd November 2010, 05:15
  3. Tree model examples
    By elcuco in forum Qt Programming
    Replies: 5
    Last Post: 18th October 2009, 23:44
  4. Writing a Tree model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 16th January 2009, 11:22
  5. Editable Tree Model
    By kloffy in forum Qt Programming
    Replies: 5
    Last Post: 10th October 2007, 15:49

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.