Results 1 to 6 of 6

Thread: Serialize myClass

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Serialize myClass

    Hi,

    Actually I have a class that I implemented a "save" method to serialize my class.

    Now I have created a QLinkedList<MyClass*>. How I have to do to save this list? I know that I can do something like this:
    Qt Code:
    1. ...
    2. ds << qMyLinkedList;
    To copy to clipboard, switch view to plain text mode 

    What really does this? Have I to reimplement any method?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialize myClass

    As a general advise: use the debugger and walk through and you will see, what happens.
    Or browse the source code - Qt is open source, though it's easy to see how things are implemented.

    Back to your question:
    Qt Code:
    1. template <typename T>
    2. QDataStream& operator<<(QDataStream& s, const QList<T>& l)
    3. {
    4. s << quint32(l.size());
    5. for (int i = 0; i < l.size(); ++i)
    6. s << l.at(i);
    7. return s;
    8. }
    To copy to clipboard, switch view to plain text mode 
    This is the implementation of the operator you are talking about. (qdatastream.h:239 - Qt 4.3.2)
    As you see the size of the list is written to the stream. Next all elements are written.
    The elements need to implement the operator<< and operator >>.
    Have all look at qdatetime.h and qdatetime.cpp how this looks like:
    Qt Code:
    1. class QDate
    2. ...
    3. friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
    4. friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const QDate &date)
    2. {
    3. return out << (quint32)(date.jd);
    4. }
    5.  
    6. ...
    7.  
    8. QDataStream &operator>>(QDataStream &in, QDate &date)
    9. {
    10. quint32 jd;
    11. in >> jd;
    12. date.jd = jd;
    13. return in;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Good luck,

    Tom

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serialize myClass

    Note that you are using POINTERS, so if you use the serialization mechanism of the list, you'll serialize pointers instead of objects, and that's surely not what you want. You have to either create a list of objects or do the serialization manually (for instance by subclassing QList<MyClass*> and reimplementing its stream operators in a fashion similar to what DeepDiver suggested).

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serialize myClass

    Ok,

    Thank you very much,
    Òscar Llarch i Galán

  5. #5
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialize myClass

    Quote Originally Posted by wysota View Post
    Note that you are using POINTERS, so if you use the serialization mechanism of the list, you'll serialize pointers instead of objects, and that's surely not what you want. You have to either create a list of objects or do the serialization manually (for instance by subclassing QList<MyClass*> and reimplementing its stream operators in a fashion similar to what DeepDiver suggested).
    You are right! I didn't see NyAw declared a list of pointers.

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serialize myClass

    Hi,
    Thanks to you. I had wrote my code and works fine. I write "myList.count()" and then I do a "for" writing all the objects. The inverse process is done in open method.

    Thanks,
    Òscar Llarch i Galán

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.