Results 1 to 4 of 4

Thread: QVariant streaming from QDataStream

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QVariant streaming from QDataStream

    Hi
    I'm using Qt 4.3.3 and trying to stream binary data (read from an arbitrary file). Therefore I started with an example as found in the Qt documentation just a few lines of code:

    Qt Code:
    1. #include <QVariant>
    2. #include <QDataStream>
    3. #include <QString>
    4. #include <QStringList>
    5. #include <QFile>
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QFile file("./test");
    11. file.open(QIODevice::ReadOnly);
    12.  
    13. QVariant v(123); // The variant now contains
    14. int x = v.toInt(); // x = 123
    15.  
    16. qDebug("x=%#x", x); // prints x
    17.  
    18. QDataStream in(&file);
    19.  
    20. // prints "Type is int"
    21. qDebug("Type is %s, userType=%d", v.typeName(), v.userType());
    22.  
    23. in >> v; // *Should* reads an Int variant
    24.  
    25. int z = v.toInt(); // z = 0 !!!!
    26. // Type is no longer Int variant:
    27. qDebug("Type is %s, userType=%d", v.typeName(), v.userType());
    28.  
    29. int n;
    30. in >> n; // Reads a normal int
    31.  
    32. qDebug("n=%#x", n); // this just works
    33.  
    34. return 1;
    35. }
    To copy to clipboard, switch view to plain text mode 

    Here the few lines produce the following output:

    markus@athlon:~/Test/bin> ./test
    x=0x7b
    Type is int, userType=2
    QVariant::load: unable to load type 1061506118.
    Type is (null), userType=1061506118
    n=0x1010000
    markus@athlon:~/Test/bin>

    So it looks like the QVariant type is lost somehow during the call to the operator>>. Does anybody have an idea why the type has suddenly the value 1061506118 instead of 2?


    Best regards, Markus
    Last edited by wysota; 25th February 2008 at 23:32. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QVariant streaming from QDataStream

    Are you aware of the fact that QDataStream is a serialization mechanism and not a general purpose binary stream? If you write an integer to the stream, you can't read a variant (and vice versa).

  3. #3
    Join Date
    Feb 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVariant streaming from QDataStream

    Hi
    Thank you very much for your quick answer. Indeed I wasn't aware of this
    Interestingly reading single characters out of a binary file worked for me with the streaming operator. But this is problably because then no endian conversion is needed.
    Anyway, this means that both, QVariant's type and QVariant's value, are streamed to/from the file and I've to go with readRawData() to read a predefined binary format.

    Best regards, Markus

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QVariant streaming from QDataStream

    std::ifstream might be an option as well.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.