3

Hey all (out there :).
Which way is the best for writing a custom class to a file in Qt?
Thank you in advance.
Matthias

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Matthias -_-
  • 101
  • 1
  • 10
  • possible duplicate of [Serialization with Qt](http://stackoverflow.com/questions/2570679/serialization-with-qt).Look at this post, it may contain additional information. – UmNyobe Jan 17 '13 at 10:06

1 Answers1

5

EDIT: Question has been already asked. Serialization with Qt

The best way is to serialize using QDataStream. For a given class MyClass, you need to define new stream operators

QDataStream &operator<<(QDataStream &, const MyClass &);
QDataStream &operator>>(QDataStream &, MyClass &);

QDataStream is already capable of writing several Qt classes, mostly collections and other convenients classes like QImage, etc.... Note that you cannot serialize any subclass of QObject. There are reasons :), but limit yourself to the explanation that QObject itself doesn't provide neither copy constructors nor in\out stream functions like the one above.

Note that if your custom class derive a class already providing serialization, you need to call the stream operators for this superclass (the same principle as calling the constructor of the superclass when constructing the subclass).

Community
  • 1
  • 1
UmNyobe
  • 22,539
  • 9
  • 61
  • 90