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 to write CSV file in Qt?
Forum Updated to NodeBB v4.3 + New Features

How to write CSV file in Qt?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 5 Posters 52.1k Views 1 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.
  • M Offline
    M Offline
    mathi77in
    wrote on 9 May 2012, 08:26 last edited by A Former User 11 Feb 2016, 19:08
    #1

    Hi,

    how to write data from specific row in CSV file. i want write some data for (2column 1row) . how to write? any sample code or url pls.

    Regards,
    M.Mathivanan

    1 Reply Last reply
    0
    • W Offline
      W Offline
      Woody
      wrote on 9 May 2012, 08:45 last edited by
      #2

      @QString filename = QFileDialog::getSaveFileName(this, "DialogTitle", "filename.csv", "CSV files (.csv);;Zip files (.zip, *.7z)", 0, 0); // getting the filename (full path)
      QFile data(filename);
      if(data.open(QFile::WriteOnly |QFile::Truncate))
      {
      QTextStream output(&data);
      output << "'your csv content';'more csv content'"
      }
      @
      that's the way i do it

      File not found. Nobody leave the room!

      1 Reply Last reply
      1
      • D Offline
        D Offline
        DerManu
        wrote on 9 May 2012, 08:49 last edited by
        #3

        You write CSV files like any other text file with QTextStream.
        @
        double value1(10);
        double value2(13.2);
        QFile file("./file.csv");
        if (file.open(QFile::WriteOnly|QFile::Truncate))
        {
        QTextStream stream(&file);
        stream << value1 << "\t" << value2 << "\n"; // this writes first line with two columns
        (...)
        file.close();
        }@

        1 Reply Last reply
        2
        • A Offline
          A Offline
          andre
          wrote on 9 May 2012, 10:35 last edited by
          #4

          Or, you use LibQxt's "QxtCsvModel":http://libqxt.bitbucket.org/doc/0.6/qxtcsvmodel.html class. It provides an [[doc:QAbstractItemModel]] type interface for CSV files.

          1 Reply Last reply
          1
          • I Offline
            I Offline
            iamantony
            wrote on 31 Oct 2016, 08:56 last edited by iamantony 3 Jul 2017, 08:23
            #5

            Try qtcsv library for reading and writing csv-files. I tried to make it small and easy-to-use. See Readme file for library documentation and code examples.

            Quick Example:

            #include <QList>
            #include <QStringList>
            #include <QDir>
            #include <QDebug>
            
            #include "qtcsv/stringdata.h"
            #include "qtcsv/reader.h"
            #include "qtcsv/writer.h"
            
            int main()
            {
                // prepare data that you want to save to csv-file
                QStringList strList;
                strList << "one" << "two" << "three";
            
                QtCSV::StringData strData;
                strData.addRow(strList);
                strData.addEmptyRow();
                strData << strList << "this is the last row";
            
                // write to file
                QString filePath = QDir::currentPath() + "/test.csv";
                QtCSV::Writer::write(filePath, strData);
            
                // read data from file
                QList<QStringList> readData = QtCSV::Reader::readToList(filePath);
                for ( int i = 0; i < readData.size(); ++i )
                {
                    qDebug() << readData.at(i).join(",");
                }
            
                return 0;
            }
            
            1 Reply Last reply
            4

            • Login

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