Unsolved How export a QtableView that has an image to .csv
-
I have written a code that exports a QTableView (that contains an image among some other data) to a .csv file. what happens is, that when I try export the QTableView to a .csv file, everything show except the image.
Does anybody now why?
void PLMainWindow::on_actionExport_As_Excel_triggered() { QString filters("CSV files (*.csv);;All files (*.*)"); QString defaultFilter("CSV files (*.csv)"); QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(), filters, &defaultFilter); QFile file(fileName); QAbstractItemModel *model = ui->tableView->model(); if (file.open(QFile::WriteOnly | QFile::Truncate)) { QTextStream data(&file); QStringList strList; for (int i = 0; i < model->columnCount(); i++) { if (model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString().length() > 0) strList.append("\"" + model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString() + "\""); else strList.append(""); } data << strList.join(",") << "\n"; for (int i = 0; i < model->rowCount(); i++) { strList.clear(); for (int j = 0; j < model->columnCount(); j++) { if (model->data(model->index(i, j)).toString().length() > 0) strList.append("\"" + model->data(model->index(i, j)).toString() + "\""); else strList.append(""); } data << strList.join(",") + "\n"; } file.close(); } }
-
a csv file is pure text, it has no support for images. You can try to serialise images using
QImage::save
on aQBuffer
operating on aQByteArray
and then save theQByteArray::toBase64
but it is still not a standard type.If you don't wnat to write the serialiser/deserialiser you can use this library in its "model serialisation" section
-
@VRonin
By the way - does the
https://github.com/VSRonin/QtXlsxWriter
support images ? -
@mrjj It does if OP's objective is to export to excel but new users should use https://github.com/QtExcel/QXlsx rather than the library you linked