Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How export a QtableView that has an image to .csv
Forum Updated to NodeBB v4.3 + New Features

How export a QtableView that has an image to .csv

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 903 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Busdago
    wrote on last edited by
    #1

    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();
    
                    }
    
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      mrjjM 1 Reply Last reply
      2
      • VRoninV VRonin

        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

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @VRonin
        By the way - does the
        https://github.com/VSRonin/QtXlsxWriter
        support images ?

        VRoninV 1 Reply Last reply
        0
        • mrjjM mrjj

          @VRonin
          By the way - does the
          https://github.com/VSRonin/QtXlsxWriter
          support images ?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved