I've set up a QAbstractItemModel and filled that with data. My QTreeView widget displays every data in that model properly.
Now, I would like to store that model serialized in a binary file (and later of cource load that binary file back into a model). Is that possible?
The same way you serialize anything, just implement an operator or method which writes each data member to a data stream in sequence.
The preferable format is to implement those two operators for your types:
Following that pattern will allow your types to be "plug and play" with Qt's container classes.
QAbstractItemModel
does not (or should not) directly hold the data, it is just a wrapper to an underlying data structure. The model only serves to provide an interface for a view to access the data. So in reality you shouldn't serialize the actual model, but the underlying data.As of how to serialize the actual data, it depends on the format of your data, which as of now remains a mystery. But since it is a
QAbstractItemModel
I assume it is a tree of some sort, so generally speaking, you have to traverse the tree and serialize every object in it.Make a note that when serializing a single object, the serialization and deserialization are a blind sequence, but when dealing with a collection of objects, you may have to account for its structure with extra serialization data. If your tree is something like an array of arrays, as long as you use Qt's container classes this will be taken care of for you, all you will need is to implement the serialization for the item type, but for a custom tree you will have to do it yourself.