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. cannot get QDataStream::setVersion to work
Forum Updated to NodeBB v4.3 + New Features

cannot get QDataStream::setVersion to work

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 297 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.
  • P Offline
    P Offline
    Poldi
    wrote on last edited by
    #1

    Maybe my expectation is wrong but I expect that when I set the version, that that version is used.
    I'm using Qt Creator 11.0.3 and cannot when I check the version after I created a new file, the version is always the default (the latest),

    #include <QCoreApplication>
    #include <QDebug>
    #include <QDataStream>
    #include <QFile>
    
    bool saveFile(QString path)
    {
        QFile file(path);
        if (!file.open(QIODevice::WriteOnly))
            return false;
    
        QDataStream out(&file);
        out.setVersion(QDataStream::Qt_6_5);
        out << (qint32) 0xA0B0C0D0;
        out << (qint32) 123;
    
        qInfo() << "The version is" << out.version();
    
        QString title = "The anser is 42";
        qint64 num = 42;
    
        out << title;
        out << num;
    
        file.close();
        return true;
    }
    
    bool readFile(QString path)
    {
        QFile file(path);
        if (!file.open(QIODevice::ReadOnly))
            return false;
    
        QDataStream in(&file);
        qInfo() << "Reading version " << in.version();
    
        file.close();
        return true;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QString file = "test.txt";
    
        if (saveFile(file)) {
            qInfo() << "Saved";
            readFile(file);
        }
        return a.exec();
    }
    
    Shows the following output
    

    The version is 20
    Saved
    Reading version 21

    What am I missing?

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • P Poldi

      Maybe my expectation is wrong but I expect that when I set the version, that that version is used.
      I'm using Qt Creator 11.0.3 and cannot when I check the version after I created a new file, the version is always the default (the latest),

      #include <QCoreApplication>
      #include <QDebug>
      #include <QDataStream>
      #include <QFile>
      
      bool saveFile(QString path)
      {
          QFile file(path);
          if (!file.open(QIODevice::WriteOnly))
              return false;
      
          QDataStream out(&file);
          out.setVersion(QDataStream::Qt_6_5);
          out << (qint32) 0xA0B0C0D0;
          out << (qint32) 123;
      
          qInfo() << "The version is" << out.version();
      
          QString title = "The anser is 42";
          qint64 num = 42;
      
          out << title;
          out << num;
      
          file.close();
          return true;
      }
      
      bool readFile(QString path)
      {
          QFile file(path);
          if (!file.open(QIODevice::ReadOnly))
              return false;
      
          QDataStream in(&file);
          qInfo() << "Reading version " << in.version();
      
          file.close();
          return true;
      }
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QString file = "test.txt";
      
          if (saveFile(file)) {
              qInfo() << "Saved";
              readFile(file);
          }
          return a.exec();
      }
      
      Shows the following output
      

      The version is 20
      Saved
      Reading version 21

      What am I missing?

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Poldi said in cannot get QDataStream::setVersion to work:

      What am I missing?

      Nothing - the version is not saved in the output. You have to define your version you want to use on both sides.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • P Poldi

        Maybe my expectation is wrong but I expect that when I set the version, that that version is used.
        I'm using Qt Creator 11.0.3 and cannot when I check the version after I created a new file, the version is always the default (the latest),

        #include <QCoreApplication>
        #include <QDebug>
        #include <QDataStream>
        #include <QFile>
        
        bool saveFile(QString path)
        {
            QFile file(path);
            if (!file.open(QIODevice::WriteOnly))
                return false;
        
            QDataStream out(&file);
            out.setVersion(QDataStream::Qt_6_5);
            out << (qint32) 0xA0B0C0D0;
            out << (qint32) 123;
        
            qInfo() << "The version is" << out.version();
        
            QString title = "The anser is 42";
            qint64 num = 42;
        
            out << title;
            out << num;
        
            file.close();
            return true;
        }
        
        bool readFile(QString path)
        {
            QFile file(path);
            if (!file.open(QIODevice::ReadOnly))
                return false;
        
            QDataStream in(&file);
            qInfo() << "Reading version " << in.version();
        
            file.close();
            return true;
        }
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            QString file = "test.txt";
        
            if (saveFile(file)) {
                qInfo() << "Saved";
                readFile(file);
            }
            return a.exec();
        }
        
        Shows the following output
        

        The version is 20
        Saved
        Reading version 21

        What am I missing?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Poldi
        I'm not sure I understand either. I expected it to write a version number to the stream, which you would check at reader side. I think @Christian-Ehrlicher is saying that is not how QDataStream::setVersion() works, it does not actually write anything into the stream, so it's an in-memory thing only, affecting what code is executed.

        Have you read https://doc.qt.io/qt-6/qdatastream.html#versioning? If I understand right, the example code there shows how you can/should introduce your own, arbitrary field in the stream which the reader can use to recognise the writer's version for this purpose.

        1 Reply Last reply
        0

        • Login

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