I am implementing a method in my class, which will write out data from a TableView Object to a CSV file. However when the program runs, the program writes the data to the file on a USB drive at a very slow rate(3 or 4 seconds), but works fine with the system's internal drive. Is this because i have not used flush() or close(), after writing the file ??
Here is my code
bool ThicknessCalibrationDataDisplay::WriteCSVFileChanges()
{
QModelIndex tableViewModelindex = tableViewModel_->index(0,0);
QFile file(CSVFileName_);
if(!file.exists())
return false;
if(!file.open(QIODevice::WriteOnly))
return false;
for(int i = 0; i < totalRows_ ; i++)
{
for(int j = 0 ; j < totalColumns_; j++)
{
tableViewModelindex = tableViewModel_->index(i,j);
qDebug()<<tableViewModelindex.data();
QString text = tableViewModelindex.data().toString();
QTextStream OutputStream(&file);
if(j == totalColumns_ - 1)
OutputStream<<"\n\r";
else
OutputStream<<',';
}
}
}
This was my code earlier, and now i plan to close the file stream, for a graceful exit. The Qt API for QFile::close() says
Calls QFile::flush() and closes the file. Errors from flush are ignored.
So should i just call close(), or is it better to call flush(), log any errors and then call close() ?
Is there any other modification, that i have to make, to improve the write operation ?
QIODevice
or whatever buy you over the standard streams. – James Kanze