Results 1 to 2 of 2

Thread: Weird results when passing a QByteArray* to a method

  1. #1
    Join Date
    May 2010
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Weird results when passing a QByteArray* to a method

    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


    Qt Code:
    1. void class::callingMethod(int ID)
    2. {
    3. QByteArray *data = new QByteArray();
    4. class2Object->getData(ID, data);
    5. qDebug() << "Data length = " << data->length();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void class2::getData(int ID, QByteArray *inData)
    2. {
    3. QSqlQuery query;
    4. // ... do query
    5.  
    6. QByteArray *dbData = new QByteArray(query.value(0).toByteArray());
    7. inData = dbData;
    8.  
    9. qDebug() << "dbData Length = " << dbData->length();
    10. qDebug() << "inData Length = " << inData->length();
    11. }
    To copy to clipboard, switch view to plain text mode 
    The output is:

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


  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Weird results when passing a QByteArray* to a method

    The output is correct as "data" still points to the original byte array. There is no point in using pointers here, anyway.

    Qt Code:
    1. void cls::callingMethod(int id){
    2. QByteArray data = class2Object->getData(id);
    3. }
    4.  
    5. QByteArray cls2::getData(int id){
    6. QSqlQuery q(...);
    7. // ...
    8. return q.value(0).toByteArray()'
    9. }
    To copy to clipboard, switch view to plain text mode 

    Or optionally if you really want to reuse an external byte array:
    Qt Code:
    1. void cls::callingMethod(int id){
    2. QByteArray data;
    3. class2Object->getData(id, data);
    4. }
    5.  
    6. void cls2::getData(int id, QByteArray &ba){
    7. QSqlQuery q(...);
    8. // ...
    9. ba = q.value(0).toByteArray();
    10. }
    To copy to clipboard, switch view to plain text mode 

    ...or with a pointer...

    Qt Code:
    1. void cls::callingMethod(int id){
    2. QByteArray data;
    3. class2Object->getData(id, &data);
    4. }
    5.  
    6. void cls2::getData(int id, QByteArray *ba){
    7. QSqlQuery q(...);
    8. // ...
    9. *ba = q.value(0).toByteArray();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 25th November 2010, 11:37
  2. QXmlQuery query results
    By JovianGhost in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2010, 03:02
  3. Replies: 9
    Last Post: 25th July 2009, 13:27
  4. Replies: 2
    Last Post: 27th March 2007, 12:09

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.