Results 1 to 7 of 7

Thread: Saving data from QStandardItemModel to harddrive

  1. #1
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Saving data from QStandardItemModel to harddrive

    Hi,

    I have a QTableView (please see attached) that is set with a QStandardItemModel, its all working properly, and now I would like to try and save the data in the "model" to the harddrive.

    Im pretty new to Qt and not really sure where to begin, I imagine I would set up a button as a signal and attach this to some kind of "save dialog" slot, but I'm not really sure... also does the model data have to be saved in a specific format?


    Thanks
    Attached Images Attached Images
    Last edited by Ferric; 18th January 2010 at 22:41.

  2. #2
    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: Saving data from QStandardItemModel to harddrive

    You can save the data any way you like. For a simple approach you could iterate over the rows and columns of the model and write to a QDataStream something like:
    Qt Code:
    1. QFile file(filename);
    2. QDataStream out(&file);
    3. out << quint32(MagicNumber); // lets you verify the file type/version on read
    4. for (int row = 0; row < model.rowCount(); row++ ) {
    5. for (int col = 0; col < model.columnCount(); col++) {
    6. // do something to fetch the item data
    7. out << quint16(row) << quint16(col) << relevant bits of the data;
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Saving data from QStandardItemModel to harddrive

    Quote Originally Posted by Ferric View Post
    Im pretty new to Qt and not really sure where to begin, I imagine I would set up a button as a signal and attach this to some kind of "save dialog" slot, but I'm not really sure... also does the model data have to be saved in a specific format?
    Setting up a button and using its QAbstractButton::clicked signal to detect when it is pushed is correct, but you connect that signal to a slot that you will add to your main window class. In that slot, you will do whatever you need to save the data, which might include calling QFileDialog::getSaveFileName to get a file name and location from the user to which to save.

    As far as I can see, the Qt model classes don’t provide any “serialization” of their own, so it will be up to your code to traverse the model in such a way that you write out both the data needed to reconstruct each QStandardItem and whatever information you need to add them to the model in the correct places (that is, that you can figure out the row and column — and parent, when a tree structure is used — of each data item when you read it back). Since the data store for each item in a QStandardItemModel is a QStandardItem, there is an easy way to write and read the data for each item, using QStandardItem::write and QStandardItem::read.

    These functions are not likely to be the most efficient in either time or space, but they provide a simple, general method that should be practical for small tables. For large data structures, you probably wouldn’t be using QStandardItem and QStandardItemModel anyway; you’d subclass QAbstractItemModel and design a custom backing store. In that case you would also have to work out a custom method of reading and writing the backing store.

  4. #4
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Saving data from QStandardItemModel to harddrive

    Hi,

    Thanks for the replies, I have the following code working well,
    Qt Code:
    1. void Loader::saveToFile()
    2. {
    3. QString fileName = QFileDialog::getSaveFileName( this, tr("Save File As..."), QDir::homePath(), tr("txt (*.txt)"));
    4. if (fileName.isEmpty())
    5. return;
    6. else
    7. {
    8. QFile file(fileName);
    9. if (!file.open(QIODevice::WriteOnly))
    10. {
    11. QMessageBox::information(this, tr("Unable to open file"), file.errorString());
    12. return;
    13. }
    14. // Need to find a way to clear all present data in file, if the file already exists.
    15. QDataStream out(&file);
    16. out.setVersion(QDataStream::Qt_4_3);
    17. out << channels; // "channels" is a QMap<QString, QString> which has the relevant information already inserted.
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    The problem I now have is that if I open a file that already exists, I overwrite my data to this file, but any original data that is not overwritten still remains... what I need is a way to delete all original data in the file before I write my new data.

    Is there a simple way to delete all data in a file?

    Thanks

  5. #5
    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: Saving data from QStandardItemModel to harddrive

    QFile::resize() could be what you are looking for. Set the size to zero before writing (or you could try to set the size to QFile::pos() at the end of writing). Alternately, test for the existence of the file and use QFile::remove() before opening.

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

    Ferric (20th January 2010)

  7. #6
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Saving data from QStandardItemModel to harddrive

    The code below works perfectly, cheers!

    Qt Code:
    1. file.resize(0);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    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: Saving data from QStandardItemModel to harddrive

    Had a bit of a D'oh! moment... you could open with "QFile::WriteOnly | QFile::Truncate" in the first place.

Similar Threads

  1. QStandardItemModel Help
    By frenk_castle in forum Newbie
    Replies: 1
    Last Post: 16th January 2010, 17:54
  2. Saving hierarchical structures of data.
    By psih128 in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2009, 08:33
  3. Saving pure plot data to image file
    By Debilski in forum Qwt
    Replies: 4
    Last Post: 7th April 2009, 17:02
  4. Display data from QStandardItemModel in QTreeView
    By jprice01801 in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2007, 09:34
  5. Record update windowd entered data saving
    By MarkoSan in forum Qt Programming
    Replies: 56
    Last Post: 18th January 2006, 18:50

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.