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