Important: Please read the Qt Code of Conduct -

Best way to create and save QVector to CSV file

  • Hello how I can easy create new .csv files and save QVectors to next columns.
    For example

    QVector v1 = (1,2,3,4,5);
    QVector v2 = (6,7,8,9,10);

    csv file:

    SOME text in first row

  • Lifetime Qt Champion

    Why do you need it to be stored in different vectors if they belong together ?

  • I don't know why :D but data columns can't be mixed up. I want use timer to call save vectors function to file.

  • Lifetime Qt Champion

    But what does it/the vectors represent ?
    You could use QPoint and one list or use a custom structure.

    Anyway, saving is pretty easy when CSV

    Something like. Its not tested and fast written so there will be syntax Terros :)

    #include <QFile>
    #include <QTextStream>
    void Save( QVector & v1 , QVector & v2  )
        QString filename = "Data.txt";
        QFile file(filename);
        if ( {
            QTextStream stream(&file);
           for (int c=0; c < v1.size(); c++ ) // v1 and v2 better be same size!
            stream << v1[c] << "," << v2[c]   << endl;

  • First column Time hh:mm:ss:zz
    2,3,4,5,6,7,8,9,10,11,12,13 column floats data i add one row per 1 ms how often I shoud save QVectors to CSV and clear memory?

  • Lifetime Qt Champion

    How often you save really depends on the goals.
    do you want save often to prevent data loss, like pc loose power?
    or do you have memory constraints so you must get rid of data fast ?

  • I'm affraid of memory constraints and I don't want block data reading.

  • Lifetime Qt Champion

    But how much memory does target have?

    • I don't want block data reading.
      Im not sure what you mean here.

  • Maybe 512MB RAM.

  • Lifetime Qt Champion

    ok. thats not that tight.
    I assume its a light OS also ?

    I would just test it out and measure how much ram one "row" costs.
    Then you can have a pretty good idea on how often u must save.

    What kind of disk will you save to ?
    Cf cards if not industrial, can be written to death surprisingly fast even
    the later generations have become much better.

    You could also leave the file open and write each row as its complete but
    that is risky in case of power failure. (all be gone)

  • Thanks for help I test time to save csv later, but i now have small problem
    I have two methods
    CreateFile() -> Save only first line with configuration
    SaveData() -> save all data in columns
    But I don't have first line only data I think that I create file two times.


    void createFile(QString startParameters)
         filename = QString("%1.csv").arg(QDateTime::currentDateTime().toString("ddMMyyyy-hh_mm_ss"));
         QFile file(filename);
         if ( )
              QTextStream stream( &file );
              stream << startParameters << endl<<endl;
    void saveDataToCSV(QVector<float>x1 , QVector<float> x2 , QVector<float> x3, QVector<float> x4)
        QFile file(filename);
           if ( {
               QTextStream stream(&file);
              for (int c=0; c < x1.size(); c++ ) 
               stream << x1[c] << "," << x2[c]  << "," << x3[c]  << "," << x4[c]  << endl;


    QString startParameters;
        QString filename;
        QVector<float> x1;
        QVector<float> x2;
        QVector<float> x3;
        QVector<float> x4;

  • Lifetime Qt Champion

    If you mean to keep data in file, you must tell it so
    -> | QIODevice::Append )

    else it will clear the file and then start writing.

  • You are the best! :D

Log in to reply