5

Recently I was working on a gui application & I wanted to save the data of QTableView in a .csv or .txt file. I Used the guidance received during this question which made me think if the reverse is also possible; i.e. if the QTableView can be populated from a .csv or .txt file. Once again I would prefer staying with a model based design such as QTableView instead of item based QTableWidget.

Any code-snippet or tutorial-documentation would be really helpful.

Community
  • 1
  • 1
RicoRicochet
  • 2,249
  • 9
  • 28
  • 53

1 Answers1

7

Consider a test.csv file (it could be generated by any text editor):

enter image description here

And the textstream behind is (if generated by programming):

1,2,3,\n4,5,6,\n7,8,9,\n10,11,12,\n13,14,15,

If opened in Microsoft Office Excel, it might looked like:

CSV values as displayed in Excel


To read this .csv file to the model of your QTableView:

QStandardItemModel *model = new QStandardItemModel;

QFile file("test.csv");
if (file.open(QIODevice::ReadOnly)) {
    
    int lineindex = 0;                     // file line counter
    QTextStream in(&file);                 // read to text stream
    
    while (!in.atEnd()) {                           
   
        // read one line from textstream(separated by "\n") 
        QString fileLine = in.readLine();  
                                         
        // parse the read line into separate pieces(tokens) with "," as the delimiter
        QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts); 
        
        // load parsed data to model accordingly
        for (int j = 0; j < lineToken.size(); j++) {
            QString value = lineToken.at(j);
            QStandardItem *item = new QStandardItem(value);
            model->setItem(lineindex, j, item);
        }

        lineindex++;   
    }

    file.close();
}

(You could manipulate the code to meet you table format)


[Result]

CSV values as displayed in QTableView

Community
  • 1
  • 1
Tay2510
  • 5,748
  • 7
  • 39
  • 58
  • i guess u did a typo,it should be, QString value = lineToken.at(j); i am getting a completely blank table in the end, data did not get populated :( edited--- sorry, sorted out. I forgot to direct output to my tableview. – RicoRicochet Dec 08 '14 at 09:37
  • You should check if you successfully loaded the `.csv` file. Use qDebug() to see if the file is loaded. – Tay2510 Dec 08 '14 at 09:53
  • no it was fine, i just forgot to direct the output to my tableView, the tableView was not getting the data thats why it was blank. now its fine. thanks a lot. – RicoRicochet Dec 08 '14 at 09:55
  • 1
    @AmarjitBiswas OK. But remember it's just a simple demo that helps you capture the main idea of parsing a `.csv` file. You should modify the code and define the data loading methods yourself according to your actual data format. – Tay2510 Dec 08 '14 at 09:58
  • I found that this demo does really help capture the big idea, despite it being so old. A quick note as of this comment ``QStringList lineToken = fileLine.split("\t", QString::SkipEmptyParts);`` will give a warning due to ``.split`` being deprecated. – als0052 Jun 02 '22 at 10:45
  • Useful code and very descriptive. – coterobarros Jul 08 '22 at 17:37