Results 1 to 5 of 5

Thread: How to write ByteArray into a text file?

  1. #1
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default How to write ByteArray into a text file?

    How to write ByteArray into a text file?

    I tried the code below but it is not working.
    Code:
    Qt Code:
    1. QString filename = "out.txt";
    2.  
    3. QModelIndex index = view->currentIndex();
    4. QSqlRecord record = model->record(index.row());
    5. int id = record.value(0).toInt();
    6.  
    7. QSqlQuery query;
    8.  
    9. query.exec("SELECT mesh_file FROM MESH_REPOSITORY WHERE scan_id = " + QString::number(id) );
    10. if(query.next())
    11. {
    12.  
    13. QByteArray meshbyte = query.value(0).toByteArray();
    14. QFile file(filename );
    15. if (file.open(QIODevice::WriteOnly))
    16. {
    17. file.write(meshbyte);
    18. file.close();
    19. }
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

  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: How to write ByteArray into a text file?

    Please define "not working".
    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.


  3. #3
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write ByteArray into a text file?

    May be it don't work because you forgot 'query.first()' after the SELECT statement?

  4. #4
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write ByteArray into a text file?

    Quote Originally Posted by wysota View Post
    Please define "not working".
    not working = no out.txt generated in the working folder.

  5. #5
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: How to write ByteArray into a text file?

    Do query.exec(), query.next() and file.open() return true?

Similar Threads

  1. conversion from bytearray to int produce zero
    By daemonna in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2010, 23:52
  2. Write colored text
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2010, 12:55
  3. How do i write to Text Edit?
    By ambang_10111 in forum Newbie
    Replies: 1
    Last Post: 6th November 2009, 09:15
  4. Read ByteArray
    By kewlcode in forum Qt Programming
    Replies: 19
    Last Post: 5th April 2009, 10:41
  5. Replies: 1
    Last Post: 3rd September 2008, 14:16

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.