Results 1 to 4 of 4

Thread: QVariant streaming from QDataStream

Threaded 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 22:32. Reason: missing [code] tags

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.