7#include <QAbstractItemModel>
12 : QObject(parent), filename(_filename), model(nullptr) {}
27static void writeValue(QTextStream &f,
const QString &value) {
28 QString escaped = value;
29 escaped.replace(
'"',
"\"\"");
30 f <<
"\"" << escaped <<
"\"";
43 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
46 QTextStream out(&file);
50 numRows =
model->rowCount();
54 for (
int i = 0; i <
columns.size(); ++i) {
63 for (
int j = 0; j < numRows; ++j) {
64 for (
int i = 0; i <
columns.size(); ++i) {
77 return file.error() == QFile::NoError;
CSVModelWriter(const QString &filename, QObject *parent=nullptr)
bool write()
Perform export of the model to CSV.
void setModel(const QAbstractItemModel *model)
void addColumn(const QString &title, int column, int role=Qt::EditRole)
const QAbstractItemModel * model
static void writeValue(QTextStream &f, const QString &value)
static void writeSep(QTextStream &f)
static void writeNewline(QTextStream &f)