Results 1 to 14 of 14

Thread: QDataStream and QByteArray issue

  1. #1
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDataStream and QByteArray issue

    hey guys,
    I'm trying to save a binary data as string in a QByteArray and for testing I used a simple text as my data and did the following:

    Qt Code:
    1. QByteArray str2Store;
    2. QDataStream out(&str2Store,QIODevice::WriteOnly);
    3. out<<"salam";
    4. qDebug()<<str2Store;
    To copy to clipboard, switch view to plain text mode 
    now I should be expecting the string "salam" in str2Store variable but only a " is echod by qDebug,ofcourse str2Store.length() returns 10,why is the length 10? and why str2Store is not filled with the the string "salam"?

  2. #2
    Join Date
    Jul 2008
    Posts
    47
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream and QByteArray issue

    Quote Originally Posted by sepehr View Post
    hey guys,
    I'm trying to save a binary data as string in a QByteArray and for testing I used a simple text as my data and did the following:

    Qt Code:
    1. QByteArray str2Store;
    2. QDataStream out(&str2Store,QIODevice::WriteOnly);
    3. out<<"salam";
    4. qDebug()<<str2Store;
    To copy to clipboard, switch view to plain text mode 
    now I should be expecting the string "salam" in str2Store variable but only a " is echod by qDebug,ofcourse str2Store.length() returns 10,why is the length 10? and why str2Store is not filled with the the string "salam"?
    Because the << operator adds the length at the beginning

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream and QByteArray issue

    when Qt writes stuff like a QString to a QDataStream it is done in a binary format in such a way it can be transmitted over a network and unpacked at the receiving site. For a QString you can expect e.g. the length of the QString data to be contained. Moreover the data might be in UTF-8 or some other encoding...

    in short: no reason to expect a binary output format to be human readable

  4. #4
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream and QByteArray issue

    Quote Originally Posted by caduel View Post
    when Qt writes stuff like a QString to a QDataStream it is done in a binary format in such a way it can be transmitted over a network and unpacked at the receiving site. For a QString you can expect e.g. the length of the QString data to be contained. Moreover the data might be in UTF-8 or some other encoding...

    in short: no reason to expect a binary output format to be human readable
    what I ment by 'expecting the string "salam" ' is the binary format of the string ,so what I did was not wrong was it? in the real world scenario I want to store a picture (QImage) in that datastream

    QImage pic("c:/parva.jpg");
    out<<pic;
    and insert str2Store in a blob column of my mySql database,but when I insert str2Store into database it's 0 byte,it's empty.what's wrong?

    whole real world code:
    Qt Code:
    1. QByteArray str2Store;
    2. QDataStream out(&str2Store,QIODevice::WriteOnly);
    3. QImage pic("c:/parva.jpg");
    4. out<<pic;
    5. QString test=str2Store;
    6. QSqlQuery insertPicCom(QString("insert into pix values('%1')").arg(test));
    7. insertPicCom.exec();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataStream and QByteArray issue

    In case pix is a blob column you would need to do:

    Qt Code:
    1. QSqlQuery insertPicCom(QString("insert into pix values(?)"));
    2. insertPicCom.bindValue(0,str2Store);
    3. insertPicCom.exec();
    To copy to clipboard, switch view to plain text mode 

    If it is a text (for example varchar) column, you need to convert the bytearry to something human-readable, for example with str2Store.toHex() or str2Store.toBase64()

  6. The following user says thank you to seneca for this useful post:

    sepehr (31st January 2009)

  7. #6
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream and QByteArray issue

    Quote Originally Posted by seneca View Post
    In case pix is a blob column you would need to do:

    Qt Code:
    1. QSqlQuery insertPicCom(QString("insert into pix values(?)"));
    2. insertPicCom.bindValue(0,str2Store);
    3. insertPicCom.exec();
    To copy to clipboard, switch view to plain text mode 

    If it is a text (for example varchar) column, you need to convert the bytearry to something human-readable, for example with str2Store.toHex() or str2Store.toBase64()
    that seems to be working,now data in database is not 0 bytes,but how can I retrieve the picture back to a QImage? I tried following but doesn't work

    Qt Code:
    1. QSqlQuery test("select * from pix");
    2. test.exec();
    3. test.first();
    4. QByteArray str2recover;
    5. QDataStream out2(&str2recover,QIODevice::ReadOnly);
    6. str2recover=test.value(0).toByteArray();
    7. QImage pix2;
    8. out2>>pix2;
    To copy to clipboard, switch view to plain text mode 
    I want pix2 to be the retrieved picture but it's empty

  8. #7
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataStream and QByteArray issue

    I guess you need to create the QDataStream after filling str2recover.

    Reason: At the time you currently create the datasteam in your sample, the byte array is empty and the stream will have status ReadPastEnd instead of Ok

  9. #8
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream and QByteArray issue

    Quote Originally Posted by seneca View Post
    I guess you need to create the QDataStream after filling str2recover.

    Reason: At the time you currently create the datasteam in your sample, the byte array is empty and the stream will have status ReadPastEnd instead of Ok
    I did that but still no luck,BTW I noticed that the way I save the picture in the database takes much more space than the image size itself,I changed it to following and the size of the blob is the same as the picture now
    Qt Code:
    1. QByteArray str2Store;
    2. QImage pix("c:/parva.jpg","JPEG");
    3. QBuffer buffer(&str2Store);
    4. buffer.open(QIODevice::WriteOnly);
    5. pix.save(&buffer,"JPEG");
    6. QSqlQuery insertPicCom(QString("insert into pix values(?)"));
    7. insertPicCom.bindValue(0,str2Store);
    8. insertPicCom.exec();
    To copy to clipboard, switch view to plain text mode 
    but I still have problem how to reverse the process and retrieve the picture from database into a QImage,Any ideas?

    PS:
    I noticed that the size of QByteArray I save in database is 3056 bytes but when I retrieve it from database and put it in another QByteArray it's 5464 bytes which means sth like a header or .. has been added to it,how can I turn it to the original QByteArray?
    Last edited by sepehr; 31st January 2009 at 18:09.

  10. #9
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream and QByteArray issue

    after spending hours(!) and thanks to you guys I got what to do,taking a peek to my previous post when I noticed the size inconsistency I tried to encode the str2Store using str2Store.toHex();
    and then saved it into database and used the static method QByteArray::fromHex(str2recover);
    and decoded it and saved it in a QImage and it just works like a charm now

    I'm attaching the final code
    Attached Files Attached Files

  11. #10
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream and QByteArray issue

    Hello to everybody,

    hoping to be helpful and to learn from everybody.

    m.


    Added after 1 20 minutes:


    Thanks for your code,
    I send you my revised version for POstgresql

    inserting :

    void lbytea()
    {
    QByteArray qba;

    QImage pix("/home/pgsql/loaddir/motrice_30.png","PNG");
    QBuffer buffer(&qba);
    buffer.open(QIODevice::WriteOnly);
    pix.save(&buffer,"PNG");
    QSqlQuery insertPicCom;
    insertPicCom.prepare("insert into pix (sch,pixyb) values(:sch,ixyb)");
    qba=qba.toHex();
    insertPicCom.bindValue(":sch","PIPPO");
    insertPicCom.bindValue("ixyb",qba);
    insertPicCom.exec();
    }

    retriving


    qstrpix="select anf_pixyb from anf_flotta where anf_idmezzo = ...
    QSqlQuery qpix(qstrpix);
    if (qpix.next()) {
    qba = qpix.value(0).toByteArray();
    qba=QByteArray::fromHex(qba);
    QPixmap dixy;
    dixy.loadFromData ( qba);
    qwh->setIcon(QIcon(dixy));
    }

    m.
    Last edited by magilda; 11th November 2010 at 00:32.

  12. #11

    Default Re: QDataStream and QByteArray issue

    QByteArray data = QByteArray::fromHex("200000008305370FFFFFF63050000 8E");
    QDataStream dataStreamToReadFrom(&data, QIODevice::ReadOnly);
    QByteArray convertedData;
    dataStreamToReadFrom >> convertedData;

    QString Str = data.toHex();
    QString convertedStr = convertedData.toHex();

    Here Str and convertedStr are not same. Why?

    I want to both QByteArray data and converted Data to be the same. How to achieve that.?

  13. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDataStream and QByteArray issue

    Do not double post. Make one post, in one section of the forum only, and it will be read.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #13
    Join Date
    Apr 2021
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QDataStream and QByteArray issue

    CAN SOMEONE HELP ME!!? im using linux ubuntu
    Im getting this error
    fatal error: QTextStream: No such file or directory

    Qt Code:
    1. #include "QTextStream"
    2. #include <QDebug>
    3. QTextStream cin(stdin);
    4. QTextStream cout(stdout);
    5. QTextStream cerr(stderr);
    6. int main() {
    7. int num1(1234), num2(2345) ;
    8. cout << oct << num2 << '\t'
    9. << hex << num2 << '\t'
    10. << dec << num2
    11. << endl;
    12. double dub(1357);
    13. cout << dub << '\t'
    14. << forcesign << dub << '\t'
    15. << forcepoint << dub
    16. << endl;
    17. dub = 1234.5678;
    18. cout << dub << '\t'
    19. << fixed << dub << '\t'
    20. << scientific << dub << '\n'
    21. << noforcesign << dub
    22. << endl;
    23. qDebug() << "Here is a debug message with " << dub << "in it." ;
    24. qDebug("Here is one with the number %d in it.", num1 );
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 2nd April 2021 at 17:40. Reason: missing [code] tags

  15. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDataStream and QByteArray issue

    Use <QTextStream> not "QTextStream". #include with quotes only looks in the local directory.

    Please do not hijack a thread to make an unrelated post. Start a new thread.
    Last edited by d_stranz; 2nd April 2021 at 17:50.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 23:05
  2. QByteArray problem
    By Misenko in forum Qt Programming
    Replies: 17
    Last Post: 4th October 2008, 22:53
  3. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 21:23

Tags for this Thread

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.