PDA

View Full Version : Weird results when passing a QByteArray* to a method



agerlach
1st December 2010, 21:57
Hello,

I am having some very weird problems passing a QByteArray* to a method to retrieve BLOB data from a SQL DB. When I check the length of the QByteArray* in the method it is the size I expect, when I check it outside the method it is zero. Does anybody have any suggestions. This is probably something simple and I've just been working too long today:p




void class::callingMethod(int ID)
{
QByteArray *data = new QByteArray();
class2Object->getData(ID, data);
qDebug() << "Data length = " << data->length();
}


void class2::getData(int ID, QByteArray *inData)
{
QSqlQuery query;
// ... do query

QByteArray *dbData = new QByteArray(query.value(0).toByteArray());
inData = dbData;

qDebug() << "dbData Length = " << dbData->length();
qDebug() << "inData Length = " << inData->length();
}
The output is:



dbData Length = 2559219
inData Length = 2559219
Data Length = 0

wysota
1st December 2010, 22:05
The output is correct as "data" still points to the original byte array. There is no point in using pointers here, anyway.


void cls::callingMethod(int id){
QByteArray data = class2Object->getData(id);
}

QByteArray cls2::getData(int id){
QSqlQuery q(...);
// ...
return q.value(0).toByteArray()'
}

Or optionally if you really want to reuse an external byte array:

void cls::callingMethod(int id){
QByteArray data;
class2Object->getData(id, data);
}

void cls2::getData(int id, QByteArray &ba){
QSqlQuery q(...);
// ...
ba = q.value(0).toByteArray();
}

...or with a pointer...


void cls::callingMethod(int id){
QByteArray data;
class2Object->getData(id, &data);
}

void cls2::getData(int id, QByteArray *ba){
QSqlQuery q(...);
// ...
*ba = q.value(0).toByteArray();
}