Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Backwards-compatible QDataStream?



  • Hi, is it possible to make QDataStream's output "backwards-compatible"?

    I mean something like Google's protobuf, where when you add a field, it doesn't affect any of the apps that don't know how to deal with it. I would like to be able to add (or even remove fields) without making the whole file unusable for other/older applications.

    Thanks!



  • Use QDataStream::setVersion to set fixed minimal version



  • Unfortunately there is no visioning implemented out of the box. it's up to you to make it work with old versions.

    To be clear what I mean is if you have this old code:

    void write(QIODevice* destination)
    {
    qint32 a=0;
    qint32 c=3;
    QDataStram stream(destination);
    stream << a <<c
    }
    void read(QIODevice* source){
    qint32 a;
    qint32 c;
    QDataStram stream(source);
    stream >> a >> c;
    }
    

    and you want to change it into

    void write(QIODevice* destination)
    {
    qint32 a=0;
    qint32 b=1;
    qint32 c=3;
    QDataStram stream(destination);
    stream << a << b<< c;
    }
    void read(QIODevice* source){
    qint32 a;
    qint32 b;
    qint32 c;
    QDataStram stream(source);
    stream >> a >> b >> c;
    }
    

    There is no out-of-the-box solution to make it backward compatible


  • Moderators

    @tmladek
    normally you write your own custom version number (magic-number) at the beginning of the data stream so you know how the data structure looks like before starting reading.



  • You can use JSON or XML and applications will process only needed tags/values.



  • Another solution: Store all your data as key-value pairs in QMap<QString, QVariant> and write it to QDataStream. Next, applications will read full QMap contents from stream, then access needed values by their names.