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

  • 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 >> 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?


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

  • 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;


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

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


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.