How to write Tree model to QDataStream
I have a QAbstractModel Tree structure i want to write all of my tree Items to the stream and load them back in the same hierarchy.
Please help me with any references or code that i can refer to .
Isn't that mostly the same question as here ?
@SGaist this is more specific towards QDataStream.
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.
Reading and Writing Other Qt Classe
Also 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.
stream << *someptr;
and not just plain
stream << someptr;
as that will just save its adress.
There is also
which might be useful if using a QAbstractItemModel model.
@mrjj Thank you very much this is indeed very helpful