Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Backwards-compatible QDataStream?
Forum Update on Monday, May 27th 2025

Backwards-compatible QDataStream?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdatastreamserialization
6 Posts 5 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tmladekT Offline
    tmladekT Offline
    tmladek
    wrote on last edited by
    #1

    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!

    raven-worxR 1 Reply Last reply
    0
    • T Offline
      T Offline
      THE_MASTER
      wrote on last edited by THE_MASTER
      #2

      Use QDataStream::setVersion to set fixed minimal version

      1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • tmladekT tmladek

          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!

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          4
          • M Offline
            M Offline
            mtrch
            wrote on last edited by
            #5

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

            1 Reply Last reply
            2
            • M Offline
              M Offline
              mtrch
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved