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. Load a csv file into a QTablewidget
Forum Update on Monday, May 27th 2025

Load a csv file into a QTablewidget

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.9k Views
  • 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.
  • C Offline
    C Offline
    coilo
    wrote on last edited by coilo
    #1

    Hi everyone,
    I am working on a function that can load a csv into a QTableWidget.
    Here is the QTableWidget where I would like to load my csv File.

    I have done a function that works very well to save a QTableWidget into a .csv file and here is the result:

    I would now like to load it into my QTableWidget. I tested this code, but the data are not displayed.

        QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), NULL, ("csv File(*.csv)"));
        QString data;
        QFile importedCSV(fileName);
        QStringList rowOfData;
        QStringList rowData;
        data.clear();
        rowOfData.clear();
        rowData.clear();
    
        if (importedCSV.open(QFile::ReadOnly))
        {
            data = importedCSV.readAll();
            rowOfData = data.split("\n");           //Value on each row
            importedCSV.close();
        }
    
        for (int x = 0; x < rowOfData.size(); x++)  //rowOfData.size() gives the number of row
        {
            rowData = rowOfData.at(x).split(";");   //Number of collumn
            int r=rowData.size();
            for (int y = 0; y < rowData.size(); y++)
            {
                ui->tableWidget_2->setItem(x,y,new QTableWidgetItem(rowData[y]));
            }
        }
        statusBar()->showMessage(tr("File successfully loaded."), 3000);
    

    Thanks in advance for your help !

    S 1 Reply Last reply
    0
    • C coilo

      Hi everyone,
      I am working on a function that can load a csv into a QTableWidget.
      Here is the QTableWidget where I would like to load my csv File.

      I have done a function that works very well to save a QTableWidget into a .csv file and here is the result:

      I would now like to load it into my QTableWidget. I tested this code, but the data are not displayed.

          QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), NULL, ("csv File(*.csv)"));
          QString data;
          QFile importedCSV(fileName);
          QStringList rowOfData;
          QStringList rowData;
          data.clear();
          rowOfData.clear();
          rowData.clear();
      
          if (importedCSV.open(QFile::ReadOnly))
          {
              data = importedCSV.readAll();
              rowOfData = data.split("\n");           //Value on each row
              importedCSV.close();
          }
      
          for (int x = 0; x < rowOfData.size(); x++)  //rowOfData.size() gives the number of row
          {
              rowData = rowOfData.at(x).split(";");   //Number of collumn
              int r=rowData.size();
              for (int y = 0; y < rowData.size(); y++)
              {
                  ui->tableWidget_2->setItem(x,y,new QTableWidgetItem(rowData[y]));
              }
          }
          statusBar()->showMessage(tr("File successfully loaded."), 3000);
      

      Thanks in advance for your help !

      S Offline
      S Offline
      StarterKit
      wrote on last edited by
      #2

      @coilo I strongly suspect that your tableWidget_2 has no rows and columns. You may check it by looking at rowCount() and columnCount(). I'm not sure but I don't see any code that would set size of the table.

      On the other hand - why you wouldn't create a custom model derived from QAbstractTableModel? Good example may be found here

      C 1 Reply Last reply
      0
      • S StarterKit

        @coilo I strongly suspect that your tableWidget_2 has no rows and columns. You may check it by looking at rowCount() and columnCount(). I'm not sure but I don't see any code that would set size of the table.

        On the other hand - why you wouldn't create a custom model derived from QAbstractTableModel? Good example may be found here

        C Offline
        C Offline
        coilo
        wrote on last edited by coilo
        #3

        @StarterKit Yes my tableWidget_2 has no row but it has 8 columns

        Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it

        jsulmJ S 2 Replies Last reply
        0
        • C coilo

          @StarterKit Yes my tableWidget_2 has no row but it has 8 columns

          Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @coilo Set proper row count using https://doc.qt.io/qt-5/qtablewidget.html#setRowCount before adding items

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • jsulmJ jsulm

            @coilo Set proper row count using https://doc.qt.io/qt-5/qtablewidget.html#setRowCount before adding items

            C Offline
            C Offline
            coilo
            wrote on last edited by coilo
            #5

            @jsulm Oh nice it worked well ! I have a little problem, the QTableWidget display well the data, but there are " " between the data as you can see

            However my .csv file does not contain any " "

            Do you know where the problem could be coming from?

            Btw here is the code which work :

                  QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), "/home", ("csv File(*.csv)"));
                  QFile file(fileName);
            
                   QString data;
                   QStringList rowOfData;
                   QStringList rowData;
                   data.clear();
                   rowOfData.clear();
                   rowData.clear();
            
                   if (file.open(QFile::ReadOnly))
                   {
                       data = file.readAll();
                       rowOfData = data.split("\n");  //Value on each row
                       file.close();
                   }
            
            
                   for (int x = 0; x < rowOfData.size(); x++)   //rowOfData.size() gives the number of row
                   {
                       rowData = rowOfData.at(x).split(";");  //Number of collumn
            
                       int r=rowOfData.size()-2;  //idk why but there is a lag of two
                       qDebug()<<r;
                       for (int y = 0; y < rowData.size(); y++)
                       {
                           ui->tableWidget_2->setRowCount(r);
                           ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y]));
                       }
                   }
            
            jsulmJ 1 Reply Last reply
            0
            • C coilo

              @jsulm Oh nice it worked well ! I have a little problem, the QTableWidget display well the data, but there are " " between the data as you can see

              However my .csv file does not contain any " "

              Do you know where the problem could be coming from?

              Btw here is the code which work :

                    QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), "/home", ("csv File(*.csv)"));
                    QFile file(fileName);
              
                     QString data;
                     QStringList rowOfData;
                     QStringList rowData;
                     data.clear();
                     rowOfData.clear();
                     rowData.clear();
              
                     if (file.open(QFile::ReadOnly))
                     {
                         data = file.readAll();
                         rowOfData = data.split("\n");  //Value on each row
                         file.close();
                     }
              
              
                     for (int x = 0; x < rowOfData.size(); x++)   //rowOfData.size() gives the number of row
                     {
                         rowData = rowOfData.at(x).split(";");  //Number of collumn
              
                         int r=rowOfData.size()-2;  //idk why but there is a lag of two
                         qDebug()<<r;
                         for (int y = 0; y < rowData.size(); y++)
                         {
                             ui->tableWidget_2->setRowCount(r);
                             ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y]));
                         }
                     }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @coilo said in Load a csv file into a QTablewidget:

              ui->tableWidget_2->setRowCount(r);

              Why do you call this in second loop?! You only need to do it once before the first loop.
              Try:

              ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y].trimmed()));
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply
              0
              • jsulmJ jsulm

                @coilo said in Load a csv file into a QTablewidget:

                ui->tableWidget_2->setRowCount(r);

                Why do you call this in second loop?! You only need to do it once before the first loop.
                Try:

                ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y].trimmed()));
                
                C Offline
                C Offline
                coilo
                wrote on last edited by
                #7

                @jsulm Oh yes you're right.

                About the " " problem, i have solved it. The problem was my save function which add automatically a " " between each data.
                Thanks a lot for your help !

                1 Reply Last reply
                0
                • C coilo

                  @StarterKit Yes my tableWidget_2 has no row but it has 8 columns

                  Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it

                  S Offline
                  S Offline
                  StarterKit
                  wrote on last edited by StarterKit
                  #8

                  @coilo said in Load a csv file into a QTablewidget:

                  Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it

                  I didn't propose to create QAbstractTableModel, I proposed to create your own model that will do CSV file operations and derive this new model class from QAbstractTableModel.
                  Have you checked the URL I provided? As you are new to Qt I highly recommend you to read it in order to better understand Qt approach for similar tasks. It will save you time in future. Of course you may use QTableWidget alone if your task is pretty simple.

                  1 Reply Last reply
                  1

                  • Login

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