Important: Please read the Qt Code of Conduct -

Reading Data from CSV FILE

  • HI All

    I am trying to read data from csv file and put data into QTableWidget.
    Following is my File read code

    QFile f("Book1.csv");
    if (
    //file opened successfully
    QString data;
    data = f.readAll();
    QStringList vals = data.split(',');
    This is my code where i try to add data from vals into Table Widget

    for (int row = 0; row < iRowCount; row++ )
    for (int col = 0; col < iColCount; col++)
    QTableWidgetItem *temp = new QTableWidgetItem(lstData[row] );
    table->setItem( col, row, temp );

    Problem I am facing
    In table it is adding 2 values in single cell.

    [EDIT: code formatting, please wrap in @-tags, Volker]

  • You should embrace your code snippets in '@'. Otherwise nobody can read it.

  • First, please format your code nicely. Please wrap it in @-tags or use the code button in the editor. Then indent it correctly, otherwise the code is completely unreadable and you are very likely to not get an answer at all. I've done this for you, as you're new to the forum and you might not have known that yet. But pleas keep it in mind for your subsequent posts.

    To your actual problem:
    In the first part you read the contents of the complete file (readAll) and split that by commas. This way you get one single huge line containing all the values. You should read the file line by line (using QTextStream and readLine()) and split the single lines.

    For the second part, I see no connection to the first at all.

    Please be aware that parsing a CSV file not just splitting at a comma! The values will be enclosed in quotes (") if they contain a comma and if a value contains a comma and a quote it's becoming even more complicated.

  • First, if you know the data that will be coming in your CSVs, go ahead and .split(","), it is a perfectly acceptible expedient for in-house tools. I process thousands of CSV files that are all columns of numbers or rigidly defined string values.

    Secondly, proper handling of CSV is, as Volker noted, a complicated task. If your goal is to produce a robust CSV handler, then, by all means dive in and do a good job. But, if you just want the data out of a CSV into your app, then don't reinvent a complex solution; use this very good implementation to access a CSV in a very Qt way -- implemented as a QAbstractItemModel:

    Additionally, especially if I am doing _any_thing at all with the data in the table, or, even if I'm not I tend to use Qt's models and views - I find it very orderly it keeps me from getting tangled up in keeping track of what row or column I'm supposed to be working on now.

    Here are some salient snips from one of my CSV processors...

    in constructor...

    MyModel = new QStandardItemModel();
    //this code actually had used a QSqlTableModel
    //QStandardItemModel is simple
    //actually, anything derived from QAbstractItemModel will work


    //actually, and this is important
    //if you were using libQxt's QxtCsvModel Class, you'd be done now.

    //but let's proceed for fun, anyway..
    //I found it convenient to put this QRecord on the stack, and just keep reusing it later
    // many more.....


    Sometimes I read the whole file (line by line) into a QList<QStringList>,
    but here I just process it line by line
    @void MainWindow::processCSVFile(QString filename)
    QFile file(filename);
    if ( | QIODevice::Text))
    while (!file.atEnd())
    QString line = file.readLine();

    Split each line and put the string into a QField
    which goes into QRecord
    and finally, inset into the Model

    void MainWindow::processLineFromCSV(QString line)
    QStringList strings = line.split(",");
    // many more...

    Maybe it seems heavy. But the pattern is easy and repeatable --
    and everything ends up exactly where it is supposed to.

    One last thing: I left references in the code where I
    update progress bars and status bars.
    -- this is soooooo helpful when you are batch processing a 1000 files of 8000 lines each.

  • @solarelectric: That's an interesting library! Thanks for the post.. I see some other cool features too.. JSON support!

  • Hi All
    Thanks for your valuable responses.

    I am new to QT.
    1.)Can anyone give me code to Import & Export CSV File.
    2.)How to align different widgets on MainWindow


    1. There are plenty of implementations around the internet, just use google. Otherwise you can have a look at the "specification": and write one yourself for fun/practice.

    2. Use Layouts, have a look at the "documentation": and if you have specific questions, you can always ask (try to figure it out yourself with the documention, though)

Log in to reply