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. [Solved] Deserialisation of QVector doesn't work for me
QtWS25 Last Chance

[Solved] Deserialisation of QVector doesn't work for me

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.6k 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.
  • M Offline
    M Offline
    moravas
    wrote on last edited by
    #1

    Hi People,

    I have a problem with mentioned topic. I have an custom template class and I would like it serialise and vice versa into an QDataStream. The usage of the stream looks like:
    @
    QByteArray array;

    s s0 = 7;
    QDataStream stream(&array, QIODevice::ReadWrite);
    
    stream << s0;
    stream.device()->reset();
    stream >> s0;
    

    @

    and the serialise and deserialise operators:
    @
    template<R_ARGS_DEF> QDataStream& operator<<(QDataStream& stream, const dimension<R_ARGS>& in) {
    stream << in.raw();
    QVector<s32> templateArgVector = {R_ARGS};
    stream << templateArgVector;
    return stream;
    }

    template<R_ARGS_DEF, typename typeOfValue> QDataStream& operator>>(QDataStream& stream, dimension<R_ARGS, typeOfValue>& out) {
    QVector<s32> templateArgVector = {R_ARGS};
    QVector<s32> streamArgVector;
    stream >> streamArgVector;

    if(streamArgVector != templateArgVector) {
        throw new BadDeserialisationTemplateTypeException();
    }
    
    typeOfValue tmp;
    stream >> tmp;
    out = tmp;
    return stream;
    

    }
    @

    you can see that I serialise the raw data(raw() method) and the template arguments via an QVector into the stream, and it works: if I use QFile instead of QByteArray as container for stream, the file will correct.
    The error occurred on the deserialising this QVector from the stream. After this operation would I like compare the two vector, but the result is an exception.

    I don't know: I make something wrong or it is an bug in the framework?
    Have anybody something idea?

    Regards,
    Norbert

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      It seems you stream out two objects:

      • Whatever type in.raw() is [line 2]
      • A QVector<s32> containing R_ARGS [line 4]

      On the way in your code:

      • Streams in data that was in in.raw() as if it were a QVector<s32> [line 11]... was it?
      • Compares that data to what I would expect to see in the QVector<s32>, which you have not streamed in.
      • Then expects to continue streaming in if the condition was met. I would expect the stream to be exhausted at that point.

      We have no idea what type 's', 's32', or 'dimension' is, or what R_ARGS is... which may have helped. Your template does not seem to use the template variable R_ARGS_DEF either.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moravas
        wrote on last edited by
        #3

        Hi ChrisW67,

        yes, I would like serialise two object:

        • the in.raw() gets back an doube value
        • the R_ARGS is an #define for 14 pieces int types
        • the R_ARGS_DEF is the definition for the R_ARGS

        In my sources marks the s32 an int type (s as signed, 32 as 32 bit).
        "s" is an typedef for "dimension" template class, like this:
        @
        typedef dimension<0,1,0,1,0,1,0,1,0,1,0,1,0,1,> s;
        @

        Regards,
        Norbert

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          OK, in.raw() returns a double and your stream that out first, followed by your QVector<s32>. When you stream in you do not read a double from the stream before you attempt to read your QVector<s32>.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            moravas
            wrote on last edited by
            #5

            Thank you for help. It works for me :) I closed the post.

            Regards,
            Norbert

            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