8 #include <QAbstractItemModel> 10 #include <QTextStream> 14 filename(_filename), model(0)
33 static void writeValue(QTextStream &f,
const QString &value)
35 QString escaped = value;
36 escaped.replace(
'"',
"\"\"");
37 f <<
"\"" << escaped <<
"\"";
40 static void writeSep(QTextStream &f)
45 static void writeNewline(QTextStream &f)
53 if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
55 QTextStream out(&file);
60 numRows =
model->rowCount();
64 for(
int i=0; i<
columns.size(); ++i)
70 writeValue(out,
columns[i].title);
75 for(
int j=0; j<numRows; ++j)
77 for(
int i=0; i<
columns.size(); ++i)
84 writeValue(out, data.toString());
91 return file.error() == QFile::NoError;
void addColumn(const QString &title, int column, int role=Qt::EditRole)
const QAbstractItemModel * model
CSVModelWriter(const QString &filename, QObject *parent=0)
void setModel(const QAbstractItemModel *model)
bool write()
Perform export of the model to CSV.