I am trying to read data from a QTableWidget
, and save them in a CSV file.
The values saved in the file are correct, but for the first column in every row there is an unwanted character in the beginning.
Here is my code:
void Task::on_button_Export_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this,tr("Export Task List"), "",tr("CSV Files (*.csv)"));
if (fileName.isEmpty())
return;
else
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_5_4);
int rowCount = ui->tableWidget->rowCount();
int colCount = ui->tableWidget->columnCount();
for (int i = 0; i < rowCount; i++)
{
QString str(QString::null);
/* if (i > 0)
{
str = "\n";
}*/
for (int j = 0; j < colCount; j++)
{
if (j > 0)
{
str += ",";
}
QTableWidgetItem* item = ui->tableWidget->item(i,j);
str += item->data(Qt::DisplayRole).toString();
}
str += "\n";
out << str;
}
}
}
The saved file looks like this:
ÎDPC Task
ÞMain Task
ÌWorkLoop
ÐWorkLoop
ÌIST0
ÊIST1
ÆIST2
I am using Qt 5.4.0.
Any help here would be greatly appreciated.