Important: Please read the Qt Code of Conduct -

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 ( | 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() + "\"");
                        data << strList.join(",") << "\n";
                        for (int i = 0; i < model->rowCount(); i++) {
                            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() + "\"");
                            data << strList.join(",") + "\n";

  • a csv file is pure text, it has no support for images. You can try to serialise images using QImage::save on a QBuffer operating on a QByteArray and then save the QByteArray::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

  • Lifetime Qt Champion

    By the way - does the
    support images ?

  • @mrjj It does if OP's objective is to export to excel but new users should use rather than the library you linked

Log in to reply