Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Read csv data in tablewidget ?
Qt 6.12 LTS is out! See what's new in the release blog

How to Read csv data in tablewidget ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 7.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I am very new to Qt and learning to use it . For an easy practice i thought to write a simple program which will will read the data from a csv file to QTablewideget. I have wrote a sample code to get started but unfortunately i can't set the column/row correctly and read the data line by line to the table.

    Here is the code:

    @#include <QApplication>
    #include <QMainWindow>
    #include <QTableWidget>
    #include <QTableWidgetItem>
    #include <QFile>
    #include <QString>
    #include <QStringList>
    #include <QMessageBox>
    #include <QTextStream>
    #include <QDebug>
     
    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    QMainWindow *window = new QMainWindow();
    QMessageBox msg;
     
    QFile file&#40;"Data.txt"&#41;;
    if(!file.open(QIODevice::ReadOnly&#41;&#41;
    msg.information(0,"Error!","Error opening file!",0);
     
    QTextStream in(&file);
    QStringList loadCsv;
     
    QTableWidget *myTable=new QTableWidget();
     
     
     
    while(!file.atEnd()){
     
    loadCsv<<in.readLine().split(";");
     
    myTable->setColumnCount(loadCsv.size());
    for(int row=0;row<in.readLine().size();++row){
    myTable->setRowCount(loadCsv.size()); //It reading only first line.
    for(int col=0;col<loadCsv.size();++col){
     
    myTable->setColumnCount(loadCsv.size());
    QTableWidgetItem *Items= new QTableWidgetItem(loadCsv[col]);
    myTable->setItem(row,col,Items);
     
    }
    }
    }
    qDebug()<<loadCsv;
    window->setCentralWidget(myTable);
    window->show();
    return app.exec&#40;&#41;;
    }
    

    @

    In CSV Data.txt i have following data:**
    bq. John Kl;34;1335532;CA;0444344
    Kuma jo;54;44432;NY;0322355
    Lebal ho;24;44022;NY;0110004

    Spent more than 2 days to figure it out myself but solution does not come to me since my logic is sucks. But i maybe i did
    not able to work properly whith while/for loop. This code currently setting 5 columns and 5 row and reading first line then setting the same line to first two row. I have taken screenshot if this helps: !http://oi39.tinypic.com/2a0hyqp.jpg()!

    If someone can help me with an example code(Or modification of this code) then it would be a great lession for me to study about.

    Please help me!

    Thanks

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      welcome to devnet

      A bit of proper indentation in your source would help to read it better.
      @
      QTextStream in(&file);
      QStringList loadCsv;

      QTableWidget *myTable=new QTableWidget();
      
      while(!file.atEnd()){
      
          loadCsv<<in.readLine().split(";");
       
          myTable->setColumnCount(loadCsv.size());
          for(int row=0;row<in.readLine().size();++row){
              myTable->setRowCount(loadCsv.size()); //It reading only first line.
      
              for(int col=0;col<loadCsv.size();++col){
               
                  myTable->setColumnCount(loadCsv.size());
                  QTableWidgetItem *Items= new QTableWidgetItem(loadCsv[col]);
                  myTable->setItem(row,col,Items);
                   
              }
          }
      }
      

      @

      The problem seem to be in line 11. You read a line and fill always the whole starting at the first row again.

      Note: that is just a quick guess. I did not study the code in detail.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        The solution is to re-write the cycle in the following way:

        @int rowsCount = 1;

        while(!in.atEnd())
        {
            loadCsv = new QStringList(in.readLine().split(";"));
        
            myTable->setRowCount(rowsCount);
            myTable->setColumnCount(loadCsv->size());
        
            for(int col = 0; col < loadCsv->size(); ++col)
            {
                QTableWidgetItem *items= new QTableWidgetItem(loadCsv->at(col));
                myTable->setItem((rowsCount - 1), col, items);
        
            }
        
            rowsCount++;
            delete loadCsv;
        }
        

        @

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Hi
          and another exaple, CSV-> QTableWidget

          @
          void MainWindow::readFile(){
          QFile file("file_csv.csv");
          QStringList listA;
          int row = 0;
          if (file.open(QIODevice::ReadOnly)){
          while (!file.atEnd()){
          QString line = file.readLine();
          listA = line.split(",");
          ui->tableWidget->setColumnCount(listA.size());
          ui->tableWidget->insertRow(row);
          for (int x = 0; x < listA.size(); x++){
          QTableWidgetItem *test = new QTableWidgetItem(listA.at(x));
          ui->tableWidget->setItem(row, x, test);
          }
          row++;
          }
          }
          file.close();
          }
          @

          1 Reply Last reply
          0

          • Login

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