Results 1 to 5 of 5

Thread: Saving/Loading a Qlist.

  1. #1
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Saving/Loading a Qlist.

    I have a Qlist 'list' , and list is the instance of my data type TEST
    Qt Code:
    1. class test
    2. {
    3.  
    4.  
    5.  
    6. public:
    7. int a;
    8. void lol(int t)
    9. {
    10. a=t;
    11.  
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    I want to save this qlist to a file. (say "save.bak")
    Later, when I open it, I want to be able to open it as a QList (name->'lista'), and add, modify etc.
    I will copy lista to list, and overwrite list to "save.bak"

    I am new to it. I couldn't understand how to serialize & deserialize (checked a bit online)
    so, please give an example as to how to do it.
    thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Saving/Loading a Qlist.

    Serialization always depends on the format you want to serialize to, e.g. serializing to XML or JSON, some CSV-like format or an INI-style format.

    What are your requirements? Do you need to be able to read the data from an earlier version of the program, do you need to be able to read the data from a future version of the program, does the format have to be human-readable, etc?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Saving/Loading a Qlist.

    I don't want to do any of that.
    doesn't even have to be human readable.
    I don't need compatibility too.(for now)
    this is a simple project for my school.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Saving/Loading a Qlist.

    Then the easiest way is to use QDataStream.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving/Loading a Qlist.

    The simplest way is using existing tools. QList has I/O operators for QDataStream. You need to equip your test class by the corresponding I/O operators:

    Qt Code:
    1. friend QDataStream& operator << ( QDataStream& out, const test& ob )
    2. {
    3. output ob to out;
    4. return out;
    5. }
    6.  
    7. friend QDataStream& operator >> ( QDataStream& in, test& ob )
    8. {
    9. read data from in and and initialize ob;
    10. return in;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Now, open a file (QFile), associate it with a QDataStream (say, fileio) and do fileio << list; or fileio >> list. The QList class will iterate through its contents and outputs stored items to the file. Or, it will append items stored in the file to the current contents of the list. Therefore, before reading in, do list.clear(). The output is a binary file.

Similar Threads

  1. Saving / Loading a QGraphicsScene with QXmlStream
    By lgb3 in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2011, 18:18
  2. Saving / Loading Files
    By nightroad in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2011, 14:13
  3. Replies: 20
    Last Post: 22nd February 2011, 18:01
  4. Loading and saving in-memory SQLITE databases
    By miraks in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2010, 22:24
  5. QDataStream and saving and loading QList
    By Noxxik in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2009, 23:02

Tags for this Thread

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.