Results 1 to 3 of 3

Thread: How to read a Number from the QByteArray

  1. #1
    Join Date
    Feb 2006
    Location
    Portugal
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to read a Number from the QByteArray

    Hi,

    I'm trying to get a integer from a QByteArray.
    After scaning the documentation I would say that the fowlloing piece of code should work:
    -----------
    int ntime;
    QByteArray stderr1 ("10");
    QBuffer buffer(&stderr1);
    buffer.open(QIODevice::ReadOnly);
    QDataStream in(&buffer);
    in >> ntime;
    printf("Output value %d\n",ntime);

    -------------
    I would expect the output :
    Output value 10
    but the Output is instead:
    Output value 0
    what am I doing wrong?

    best regards

    António Tomé

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a Number from the QByteArray

    QDataStream uses its own, binary format. Use QTextStream instead:
    Qt Code:
    1. int ntime;
    2. QByteArray stderr1 ("10");
    3. QBuffer buffer(&stderr1);
    4. buffer.open(QIODevice::ReadOnly | QIODevice::Text );
    5. QTextStream in(&buffer);
    6. in >> ntime;
    7. printf("Output value %d\n",ntime);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Portugal
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a Number from the QByteArray

    Many thanks,

    It worked flawless.


    António Tomé

Similar Threads

  1. Apparent error in QtCore/quuid.h
    By cwp500 in forum Qt Programming
    Replies: 11
    Last Post: 18th December 2008, 20:51
  2. QByteArray problem
    By Misenko in forum Qt Programming
    Replies: 17
    Last Post: 4th October 2008, 21:53
  3. External Lib read from QBuffer on Calback problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2008, 19:43
  4. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.