Results 1 to 7 of 7

Thread: Serializing custom data object

  1. #1
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Serializing custom data object

    Hey guys,

    i would like to serialize a custom data class to save data when my app closes and reload it after it reopens. The data is stored in an std::vector<particle_c> (particle_c is my custom data class) and i have a few 100 - 1000 particles. Each particle has properties of int, QStrings, doubles, and some eigen3/vectors (for matrix calculations). I've been thinking about the right approach to save the data and ended up with serialization. Is this the right approach? i'm not quite sure how to serialize the two vector classes. I could probably rewrite the code to make it use QVectors if thats any help. Also read a little about the boost serialization library, could that also be an option?

    thx for the input friends

  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: Serializing custom data object

    The easiest way to do that is to use QDataStream as it can directly serialize QString and other Qt types.

    You can either serialize each struct member separately in the loop over the vector or add QDataStream stream operators to the struct.

    Cheers,
    _

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

    Lumbricus (7th March 2016)

  4. #3
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Serializing custom data object

    thx for the input!

    so lets say i have delcare my data like this:

    std::vector<particle_c> particles_all;


    is this the way to implement the datastream operator for a vector?

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const std::vector<particle_c> &particles_all)
    2. {
    3.  
    4. for (int i=0; i<particles_all.size(),i++)
    5. {
    6. out << particles_all[i].mass << particles_all[i].state_rel_sun.pos(0)
    7. << quint32(particles_all[i].mass_option); //and loads of other stuff
    8. return out;
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    do i have to declare each vartype e.g. quint32(particles_all.mass_option) or is it enough if its a qt known type and its declared in a header e.g (in header of particle_c class: double mass; )
    Last edited by Lumbricus; 7th March 2016 at 16:17.

  5. #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: Serializing custom data object

    Are you sure that std::vector has members called "mass", "state:rel_sun" or "mass_option"?

    Cheers,
    _

  6. #5
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Serializing custom data object

    oh my bad, i edited the previous post. forgot the iterator of the vector. nice catch. wrote the code from the top of my head.

    Otherwise tho the idea is ok to use the loop in the operator?

  7. #6
    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: Serializing custom data object

    You should serialise the size of the vector before its content. When you come to deserialise the data this size value lets you size the vector properly and know when the vector stops and the next item you serialise starts.

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

    Lumbricus (19th March 2016)

  9. #7
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Serializing custom data object

    Thx Chris, splitting the vector beforehand and then writing the operator for a custom object worked for me!

Similar Threads

  1. Add custom object to QListWidget
    By robzanab in forum Qt Programming
    Replies: 10
    Last Post: 14th December 2015, 17:06
  2. Replies: 2
    Last Post: 12th December 2013, 14:25
  3. add custom class object to QVariantList
    By ggdev001 in forum Qt Programming
    Replies: 17
    Last Post: 26th February 2013, 07:16
  4. Can you add a custom object to a SQL database?
    By agerlach in forum Qt Programming
    Replies: 4
    Last Post: 30th June 2010, 15:51
  5. Serializing
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2008, 08:51

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.