Results 1 to 6 of 6

Thread: Append/Prepend QString to QByteArray

  1. #1
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Append/Prepend QString to QByteArray

    Hi,

    I have a question about QByteArray.

    assume that i have a source like this
    Qt Code:
    1.  
    2. QString inName = QFileDialog::getOpenFileName(this, "Load a file");
    3. QFile *fileIn = new QFile(inName);
    4. fileIn->open(QIODevice::ReadOnly);
    5.  
    6.  
    7. ba = fileIn->readAll();
    To copy to clipboard, switch view to plain text mode 

    "ba" is a QByteArray, i want to append QString after ba. How can i do that? because when i use QByteArray::append() i can't see anything change, and after i try to write ba as a file again, it is still the same like before.

    Thanks in advanced,


    ps: my file is binary(video) file

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Append/Prepend QString to QByteArray

    can you post your code???
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Append/Prepend QString to QByteArray

    Quote Originally Posted by mcosta View Post
    can you post your code???
    Qt Code:
    1.  
    2. QString inName = QFileDialog::getOpenFileName(this, "Load a file");
    3. QFile *fileIn = new QFile(inName);
    4. fileIn->open(QIODevice::ReadOnly);
    5.  
    6.  
    7. ba = fileIn->readAll();
    8. qDebug() << ba;
    9. qDebug() << ba.length();
    10. fileIn->close();
    11.  
    12. QByteArray acba = ba;
    13. QString key = "thisisakey";
    14. acba = acba.append(key);
    15. qDebug() << acba; //i hope here will go "thisisakey" appended
    16.  
    17. QString outName = QFileDialog::getSaveFileName(this, "Save a file");
    18. QFile *fileOut = new QFile(outName);
    19. fileOut->open(QIODevice::WriteOnly);
    20.  
    21. fileOut->write(acba); // i will fail save the same file as before because i have append it with key,
    22. // but the result it is success, and it means there is no change.
    23. fileOut->flush();
    24. fileOut->close();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Append/Prepend QString to QByteArray

    What does acba.size() return after line 15 compared to its result before this line?
    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.


  5. The following user says thank you to wysota for this useful post:

    ermustaqbal (2nd August 2011)

  6. #5
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Append/Prepend QString to QByteArray

    Quote Originally Posted by wysota View Post
    What does acba.size() return after line 15 compared to its result before this line?
    wysota after i print it out i have something like this :

    before append: 26246026
    after append: 26246036

    it means the key succesfully appended?


    Added after 54 minutes:


    Hohoho got it, thanks wysota you make me think about size ... thanks a lot ...
    Last edited by ermustaqbal; 2nd August 2011 at 04:01.

  7. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Append/Prepend QString to QByteArray

    size increeased of 10 (size of "thisisakey");

    This code works for me.
    Qt Code:
    1. QByteArray ba ("1234567890");
    2. qDebug () << ba;
    3.  
    4. ba = ba.append(QString("Hello World"));
    5. qDebug () << ba;
    To copy to clipboard, switch view to plain text mode 

    If your file is a binary one, writing on console is wrong because at the first 0x00 byte the output will be truncated.
    Last edited by mcosta; 2nd August 2011 at 07:25. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. How do I append and int to my QString?
    By seink in forum Newbie
    Replies: 3
    Last Post: 31st May 2015, 21:45
  2. QString::append() weirdness
    By mattc in forum Qt Programming
    Replies: 16
    Last Post: 17th October 2010, 13:02
  3. Replies: 3
    Last Post: 29th April 2010, 19:11
  4. QByteArray to QString
    By hvitual in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2009, 10:43
  5. Need help. can't append chars in QString
    By AcerExtensa in forum Qt Programming
    Replies: 6
    Last Post: 12th June 2008, 10:57

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.