Results 1 to 9 of 9

Thread: Serialization custom type

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: Serialization custom type

    Or maybe, do you have some idea how to save data? My situation is:
    I have no idea what you are trying to accomplish. Maybe if you described (in words) the user problem you are trying to solve with "pads", "keys" and so forth, it will make more sense.

    If you have a custom class or struct that you wish to serialize using the QDataStream operators, your operator methods have to serialize -all- of the members of that class or struct if you want to be able read in and restore exactly what you have written. You don't just serialize one member unless you can somehow create the values of the other members from that one member's value.

    I don't understand why you are defining a t_timeloops struct that contains another QMap as a member. If that's all it is, typedef it:

    Qt Code:
    1. typedef QMap< QString, t_keyParam > t_timeloops;
    To copy to clipboard, switch view to plain text mode 

    You still end up with a QMap< QString, QMap< QString, t_keyParam > > as your top-level data container without the extra class as a nuisance, if that is indeed what you want.

    You do not need to write any other QDataStream operators -except- the pair for t_keyParam, and you need to read / write -all- the members of that struct. The QMap operators will take care of the jigher levels for you.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serialization custom type

    Ok thanks, so this could be a final struct
    Qt Code:
    1. typedef enum t_timeloopsStatus
    2. {
    3. NOTHING = 0,
    4. UPDATED,
    5. UPLOADED,
    6. CHANGED
    7. } t_timeloopsStatus;
    8.  
    9. typedef enum t_timeloopsTime
    10. {
    11. KEY = 0,
    12. PAD
    13. } t_timeloopsTime;
    14.  
    15. typedef struct t_timeloops
    16. {
    17. QString keyTime;
    18. QString padTime;
    19. QString cylinderTime;
    20. t_timeloopsStatus status;
    21. t_timeloopsTime select;
    22. } t_timeloops;
    23.  
    24. QMap<QString, QMap<QString, t_timeloops>> _timeLoops;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const t_timeloops &timeLoops)
    2. {
    3. out << static_cast<QString>(timeLoops.keyTime) << static_cast<QString>(timeLoops.padTime) << static_cast<QString>(timeLoops.cylinderTime) << static_cast<quint16>(timeLoops.select) << static_cast<quint16>(timeLoops.status);
    4. return out;
    5. }
    6.  
    7.  
    8. QDataStream &operator>>(QDataStream &in, const t_timeloops &timeLoops)
    9. {
    10. in >> static_cast<QString>(timeLoops.keyTime) >> static_cast<QString>(timeLoops.padTime) >> static_cast<QString>(timeLoops.cylinderTime) >> (quint16&)timeLoops.select >> (quint16&)timeLoops.status;
    11. return in;
    12. }
    To copy to clipboard, switch view to plain text mode 
    It seems that the writing works, but still problem with reading and enums.

    Thanks again!, it's really helpful.
    Last edited by ado130; 6th February 2017 at 19:46.

  3. #3
    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: Serialization custom type

    The static_cast< QString > is completely unnecessary. QDataStream knows how to serialize QString types, just write out << timeloops.keyTime, etc.

    Enums are no different from int or unsigned int as far as serialization goes. Your methods should look like this (note the removal of "const" from operator>> - you can't write to a const variable):

    Qt Code:
    1. QDataStream & operator<<(QDataStream & out, const t_timeloops & timeLoops)
    2. {
    3. out << timeLoops.keyTime << timeLoops.padTime << timeLoops.cylinderTime << static_cast<quint16>(timeLoops.select) << static_cast<quint16>(timeLoops.status);
    4. return out;
    5. }
    6.  
    7. QDataStream & operator>>(QDataStream & in, t_timeloops & timeLoops)
    8. {
    9. in >> timeLoops.keyTime >> timeLoops.padTime >> timeLoops.cylinderTime;
    10. quint16 inVal;
    11. in >> inVal;
    12. timeLoops.select = static_cast< t_timeloopsTime >( inVal );
    13. in >> inVal;
    14. timeLoops.status = static_cast< t_timeloopsStatus >( inVal );
    15. return in;
    16. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    ado130 (7th February 2017)

  5. #4
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serialization custom type

    Thanks a lot d_tranz, you're great, it was really helpful.

Similar Threads

  1. Custom Type Sending
    By iprion in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2013, 11:19
  2. QVariant with Custom Type
    By kasmanit in forum Qt Programming
    Replies: 4
    Last Post: 2nd September 2012, 17:02
  3. QSettings custom class serialization problem
    By sakya in forum Qt Programming
    Replies: 1
    Last Post: 19th December 2011, 11:00
  4. QTreeView with custom type
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 25th September 2011, 20:27
  5. QVariant custom type.
    By hickscorp in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 14:23

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.