Boost serialization in Qt: is it a proper way?

2019-04-01 13:47发布

问题:

I'm thinking about serializing data in an application which is Qt-based.

Essentially what I'm going to serialize is my hierarchical model, which is composed of different classes that derive from, say, TreeModelItem:

class TreeModelItem
{
protected:
    QList<TreeModelItem *> m_children;
//...
};

Should I study boost::serialization and go on with it?

Is there any hidden wall I can hit by the way? E.g. while (de)serializing child elements, or when restoring custom singal-slot connections? I hope for the advice of experts.

回答1:

QDataStream supports (de)serialization of some popular Qt objects. You can check which ones here. The "Qt" way would be to use that.

However, there's nothing preventing you from using boost, but you will have to implement the serialization for basic objects such as QList all over again, which can be tiresome.

Note that if you have custom objects, such as your TreeModelItem, you would have to provide an operator<< of your own.

Regarding the serialization of signals/slots: afaik Qt doesn't support this atm, and I believe the Qt team has made it this way intentionally. If you're interested why, maybe this read can be helpful.



回答2:

If you are interested in boost's serialization to xml file you can check my implementation for QString, QList and QStringList: https://github.com/konserw/qarchive - from there it should be fairly easy to add implementation for other Qt calsses (forks and PRs welcome! :) )