Not a temporary table, you can use a QSortFilterProxyModel and then serialise the proxy rather than the original model.
Since your current architecture relies on the QStandardItem interface rether than the QAbstractItemModel one you'll need to do some rewriting.
To give you an idea, being QString m_csvSeparator the column separator:
#include <QStringBuilder>
QString escapedCSV(QString unexc) const
{
if (!unexc.contains(m_csvSeparator))
return unexc;
unexc.replace(QLatin1Char('\"'), QStringLiteral("\"\""));
return '\"' % unexc % '\"';
}
bool writeCsv(QTextStream& writer, const QAbstractItemModel* constModel) const
{
if (!constModel)
return false;
for (int i = 0, maxRow = constModel->rowCount(); i < maxRow ; ++i) {
for (int j = 0, maxCol = constModel->columnCount(); j < maxCol ; ++j) {
const QString roleData = constModel->index(i, j).data().toString();
if (j>0)
writer << m_csvSeparator;
if (!roleData.isEmpty())
writer << escapedCSV(roleData);
}
writer << '\n';
}
return writer.status() == QTextStream::Ok;
}