PDA

View Full Version : QDataStream and QByteArray issue



sepehr
31st January 2009, 09:49
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:



QByteArray str2Store;
QDataStream out(&str2Store,QIODevice::WriteOnly);
out<<"salam";
qDebug()<<str2Store;

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

muellerp
31st January 2009, 10:20
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:



QByteArray str2Store;
QDataStream out(&str2Store,QIODevice::WriteOnly);
out<<"salam";
qDebug()<<str2Store;

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

caduel
31st January 2009, 10:21
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

sepehr
31st January 2009, 12:24
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:


QByteArray str2Store;
QDataStream out(&str2Store,QIODevice::WriteOnly);
QImage pic("c:/parva.jpg");
out<<pic;
QString test=str2Store;
QSqlQuery insertPicCom(QString("insert into pix values('%1')").arg(test));
insertPicCom.exec();

seneca
31st January 2009, 12:47
In case pix is a blob column you would need to do:


QSqlQuery insertPicCom(QString("insert into pix values(?)"));
insertPicCom.bindValue(0,str2Store);
insertPicCom.exec();

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()

sepehr
31st January 2009, 14:29
In case pix is a blob column you would need to do:


QSqlQuery insertPicCom(QString("insert into pix values(?)"));
insertPicCom.bindValue(0,str2Store);
insertPicCom.exec();

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



QSqlQuery test("select * from pix");
test.exec();
test.first();
QByteArray str2recover;
QDataStream out2(&str2recover,QIODevice::ReadOnly);
str2recover=test.value(0).toByteArray();
QImage pix2;
out2>>pix2;

I want pix2 to be the retrieved picture but it's empty

seneca
31st January 2009, 15:32
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

sepehr
31st January 2009, 16:29
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


QByteArray str2Store;
QImage pix("c:/parva.jpg","JPEG");
QBuffer buffer(&str2Store);
buffer.open(QIODevice::WriteOnly);
pix.save(&buffer,"JPEG");
QSqlQuery insertPicCom(QString("insert into pix values(?)"));
insertPicCom.bindValue(0,str2Store);
insertPicCom.exec();

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?

sepehr
31st January 2009, 17:40
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 :D

I'm attaching the final code ;)

magilda
10th November 2010, 23:32
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,:pixyb)");
qba=qba.toHex();
insertPicCom.bindValue(":sch","PIPPO");
insertPicCom.bindValue(":pixyb",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.:)

alokraj001
24th March 2021, 17:54
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.?

d_stranz
24th March 2021, 20:13
Do not double post. Make one post, in one section of the forum only, and it will be read.

harmonyclexx
2nd April 2021, 00:15
CAN SOMEONE HELP ME!!? im using linux ubuntu
Im getting this error
fatal error: QTextStream: No such file or directory


#include "QTextStream"
#include <QDebug>
QTextStream cin(stdin);
QTextStream cout(stdout);
QTextStream cerr(stderr);
int main() {
int num1(1234), num2(2345) ;
cout << oct << num2 << '\t'
<< hex << num2 << '\t'
<< dec << num2
<< endl;
double dub(1357);
cout << dub << '\t'
<< forcesign << dub << '\t'
<< forcepoint << dub
<< endl;
dub = 1234.5678;
cout << dub << '\t'
<< fixed << dub << '\t'
<< scientific << dub << '\n'
<< noforcesign << dub
<< endl;
qDebug() << "Here is a debug message with " << dub << "in it." ;
qDebug("Here is one with the number %d in it.", num1 );
}

d_stranz
2nd April 2021, 16:41
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.