Results 1 to 9 of 9

Thread: Serialization custom type

Threaded View

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

    Default Serialization custom type

    Hello,
    I'm trying write to binary file my custom type, so I have:

    Qt Code:
    1. typedef enum t_timeloopsStatus
    2. {
    3. NOTHING = 0,
    4. UPDATED,
    5. } t_timeloopsStatus;
    6.  
    7. typedef struct t_timeloops
    8. {
    9. QString key;
    10. t_timeloopsStatus status;
    11. } t_timeloops;
    12.  
    13. QMap<QString, t_timeloops> _timeLoops;
    To copy to clipboard, switch view to plain text mode 
    I know, that I have to overload << and >> operators
    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const QMap<QString, t_timeloops> &timeloops);
    2. QDataStream &operator>>(QDataStream &in, QMap<QString, t_timeloops> &timeloops);
    To copy to clipboard, switch view to plain text mode 
    but I have no idea how will looks function for that.

    I found an original function for QMap and tried to rewrite it, but without success.
    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const QMap<QString, t_timeloops> &timeloops)
    2. {
    3. out << quint32(timeloops.size());
    4. typename QMap<QString, t_timeloops>::ConstIterator it = timeloops.end();
    5. typename QMap<QString, t_timeloops>::ConstIterator begin = timeloops.begin();
    6. while (it != begin) {
    7. --it;
    8. out << it.key() << it.value();
    9. }
    10. return out;
    11. }
    12.  
    13. QDataStream &operator>>(QDataStream &in, QMap<QString, t_timeloops> &timeloops)
    14. {
    15. QDataStream::Status oldStatus = in.status();
    16. in.resetStatus();
    17. timeloops.clear();
    18.  
    19. quint32 n;
    20. in >> n;
    21.  
    22. timeloops.detach();
    23. for (quint32 i = 0; i < n; ++i) {
    24. if (in.status() != QDataStream::Ok)
    25. break;
    26.  
    27. QString key;
    28. t_timeloops value;
    29. in >> key >> value.key >> value.pad >> value.cylinder >> value.status;
    30. timeloops.insertMulti(key, value);
    31. }
    32. if (in.status() != QDataStream::Ok)
    33. timeloops.clear();
    34. if (oldStatus != QDataStream::Ok)
    35. in.setStatus(oldStatus);
    36. return in;
    37. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help!
    Last edited by ado130; 3rd February 2017 at 18:11.

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.