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

Qt4 and Qt5 compatibility issue with QDatastream and QDateTime



  • I have some binary files that is created with an old application written in Qt4 by using QDatastream. Now I would like to read these files using a newer version of application written in Qt5. I am running into a weird scenario that only QDateTime variable can not be read correctly. It is always "invalid". Has there any major changes in how QDateTime gets serialized from Qt4 to QT5? All other primitive type of variables are all fine.

    Example1:

    QDateTime startTime;
    QString comment1; 
    
    void Read(QDataStream& in)
    {
         in >> startTime >> comment1;
    }
    

    The comment1 can be read correctly, but startTime is always invalid.

    Example2: (A even more weird situation)

    QDateTime startTime;
    QString comment1; 
    float     floatingvalue;
    
    void Read(QDataStream& in)
    {
         in >> comment1 >> startTime >> floatingvalue;
    }
    

    In this situation, both startTime and value can't be read correctly. However, if I put a quint16 variable after the Qstring variable, then "floatingvalue" can be read (even though startTime still gives wrong value). For example:

    void Read(QDataStream& in)
    {
         quint16 extra;
         in >> comment1 >> extra >> startTime >> floatingvalue;
    }
    

    I am wondering if QString is serialized differently as well?



  • Now I realized I have to set QDataStream::setVersion to QDataStream::Qt_4_7. I think the problem is solved.