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.
  • T Offline
    T Offline
    tmladek
    wrote on 1 Nov 2016, 18:10 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!

    R 1 Reply Last reply 2 Nov 2016, 06:57
    0
    • T Offline
      T Offline
      THE_MASTER
      wrote on 1 Nov 2016, 18:45 last edited by THE_MASTER 11 Jan 2016, 18:47
      #2

      Use QDataStream::setVersion to set fixed minimal version

      1 Reply Last reply
      4
      • V Offline
        V Offline
        VRonin
        wrote on 1 Nov 2016, 19:37 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
        • T tmladek
          1 Nov 2016, 18:10

          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!

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 2 Nov 2016, 06:57 last edited by raven-worx 11 Feb 2016, 06:57
          #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 3 Nov 2016, 09:34 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 3 Nov 2016, 09:44 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

              1/6

              1 Nov 2016, 18:10

              • Login

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