Results 1 to 5 of 5

Thread: Save model to file

  1. #1
    Join Date
    Apr 2010
    Location
    Rzeszów \ Poland
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Save model to file

    Hello.
    I have a problem. I want to save a QStandardItemModel objects into file.
    But... how?

    Have you got any simple idea?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Save model to file

    Loop through your data and save it to a file. But how do you have created your model (on what is it based?)?

  3. #3
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save model to file

    You should walk through the model and save on an item-by-item basis. You may open a file and use QDataStream to save the contents of each item. For example, assuming the model is a table model (for simplicity):

    Qt Code:
    1. void loadModel(QIODevice *device, QStandardItemModel *model)
    2. {
    3. QDataStream stream(device);
    4. int rowCount, columnCount;
    5. stream >> rowCount;
    6. stream >> columnCount;
    7.  
    8. for(int row = 0; row < rowCount; row++)
    9. for(int column = 0; column < columnCount; column++) {
    10. QString item;
    11. stream >> item;
    12. model->setItem(row, column, item);
    13. }
    14. }
    15.  
    16. void saveModel(QIODevice *device, QStandardItemModel *model)
    17. {
    18. QDataStream stream(device);
    19. int rowCount = model->rowCount();
    20. int columnCount = model->columnCount();
    21. stream << rowCount;
    22. stream << columnCount;
    23.  
    24. for(int row = 0; row < rowCount; row++)
    25. for(int column = 0; column < columnCount; column++) {
    26. stream << model->item(row, column)->text();
    27. }
    28. }
    29.  
    30. ...
    31.  
    32. /* populate the model */
    33.  
    34. QFile file;
    35. if(file.open(QIODevice::WriteOnly))
    36. saveModel(&file, &myModel);
    To copy to clipboard, switch view to plain text mode 
    Last edited by victor.fernandez; 10th May 2010 at 16:17. Reason: reformatted to look better

  4. #4
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save model to file

    I know that there is something like QStandardItem::write, but I have no idea how to use it.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save model to file

    QStandardItem::write only writes the data and flags of the item to a selected QDataStream, it doesn't deal with any child elements. Victor's solution looks better for that task.

Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 10:23
  2. Save QWidget as PDF file
    By chapu in forum Newbie
    Replies: 2
    Last Post: 26th April 2010, 04:35
  3. Save Icon in a file
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2009, 18:07
  4. Save qpixmap to file
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 17:36
  5. Table model save on disk
    By giusepped in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2009, 14:28

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.