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:
Code:
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"?
Re: QDataStream and QByteArray issue
Quote:
Originally Posted by
sepehr
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:
Code:
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
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
Re: QDataStream and QByteArray issue
Quote:
Originally Posted by
caduel
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:
Code:
out<<pic;
insertPicCom.exec();
Re: QDataStream and QByteArray issue
In case pix is a blob column you would need to do:
Code:
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()
Re: QDataStream and QByteArray issue
Quote:
Originally Posted by
seneca
In case pix is a blob column you would need to do:
Code:
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
Code:
test.exec();
test.first();
str2recover=test.value(0).toByteArray();
out2>>pix2;
I want pix2 to be the retrieved picture but it's empty
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
Re: QDataStream and QByteArray issue
Quote:
Originally Posted by
seneca
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
Code:
QImage pix
("c:/parva.jpg",
"JPEG");
pix.save(&buffer,"JPEG");
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?
1 Attachment(s)
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 :D
I'm attaching the final code ;)
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,: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.:)
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.?
Re: QDataStream and QByteArray issue
Do not double post. Make one post, in one section of the forum only, and it will be read.
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
Code:
#include "QTextStream"
#include <QDebug>
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 );
}
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.