How to write Tree model to QDataStream
-
Hi
Well the first step is to implement
QDataStream &operator<<(QDataStream &, const QXxx &);
QDataStream &operator>>(QDataStream &, QXxx &);
for all classes involved.
Then you need to traverse the tree and output
each parent and its children. I think QList can be directly streamed if the class it holds has << >> overrides.Most Qt types can be directly streamed so often the operator override is quite trivial and
only needs you to stream its class members.please see
https://doc.qt.io/qt-5/qdatastream.html
and section
Reading and Writing Other Qt ClasseAlso please note the easy to make mistake.
The operators << and >> takes reference to the type.
so when you stream a pointer,
you need to dereference it or it will just think "hey - its an int" and save the address.
so
stream << *someptr;
and not just plain
stream << someptr;
as that will just save its adress. -
Hi
There is also
https://github.com/VSRonin/Qt-Model-Serialisation
which might be useful if using a QAbstractItemModel model. -
Hi
There is also
https://github.com/VSRonin/Qt-Model-Serialisation
which might be useful if using a QAbstractItemModel model.