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. CSV to QTaleWidget
Qt 6.11 is out! See what's new in the release blog

CSV to QTaleWidget

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 2.2k Views 2 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.
  • D Davide00

    Hi, I am trying to read the csv file and showing the information through a dialog containing a QTableWidget, using this code:

    QFile file("users.csv");
    
        QTextStream in(&file);
        QString loadCsv;
    
        int rowsCount = 1;
    
        while(!in.atEnd())
        {
            QString line = in.readLine();
            loadCsv = line.split("\t")[rowsCount - 1];
            int pos = loadCsv.lastIndexOf(',');
            loadCsv = loadCsv.left(pos);
    
            ui->tableWidget->setRowCount(rowsCount);
            ui->tableWidget->setColumnCount(loadCsv.size());
    
            for(int col = 0; col < loadCsv.size(); ++col)
            {
                QTableWidgetItem *items= new QTableWidgetItem(loadCsv.at(col));
                ui->tableWidget->setItem((rowsCount - 1), col, items);
    
            }
    
            rowsCount++;
        }
    

    but it just shows me a blank table. What am I doing wrong?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @Davide00
    Unless you put some qDebug() statements in there, we/you do not know how many rows or columns it is encountering or what visible string you are putting in any cells....

    ui->tableWidget->setColumnCount(loadCsv.size());

    If, for whatever reason, a blank line is encountered, say at the end, this would remove all previously-added columns from the table. Do you think this is safe coding?

    D 1 Reply Last reply
    2
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #3

      @Davide00 said in CSV to QTaleWidget:

          QString line = in.readLine();
          loadCsv = line.split("\t")[rowsCount - 1];
          int pos = loadCsv.lastIndexOf(',');
          loadCsv = loadCsv.left(pos);
      

      This looks strange. You're parsing a single line here, I'm not sure why you are using rowsCount here. Is your data tab delimited or comma delimited? I would expect this to be just:

      QString delimiter = ","; //specify the delimiter of your file
      QStringList loadCsv = line.split(delimiter);
      
      D 1 Reply Last reply
      0
      • M mchinand

        @Davide00 said in CSV to QTaleWidget:

            QString line = in.readLine();
            loadCsv = line.split("\t")[rowsCount - 1];
            int pos = loadCsv.lastIndexOf(',');
            loadCsv = loadCsv.left(pos);
        

        This looks strange. You're parsing a single line here, I'm not sure why you are using rowsCount here. Is your data tab delimited or comma delimited? I would expect this to be just:

        QString delimiter = ","; //specify the delimiter of your file
        QStringList loadCsv = line.split(delimiter);
        
        D Offline
        D Offline
        Davide00
        wrote on last edited by Davide00
        #4

        @mchinand my data are delimited using "\t".
        Actually this is a code i found on the internet, i tried to modify it because, ad the end of every row i used ",\n" to start a new line.
        For example
        "name1" << "\t" << "email1" << ",\n"
        "name2" << "\t" << "email2" << ",\n" etc.
        The problem is, it worked but it showed me ' , ' at the end of every row. I tried to modify the code in order to cancel the ' , ' but now it just show me a blank table.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by mchinand
          #5

          You could try:

          auto loadCsv = line.split(",")[0].split("\t");
          

          Edit: If it's your code that is creating the CSV file(s), I would fix that as well, so you don't need this.

          1 Reply Last reply
          1
          • JonBJ JonB

            @Davide00
            Unless you put some qDebug() statements in there, we/you do not know how many rows or columns it is encountering or what visible string you are putting in any cells....

            ui->tableWidget->setColumnCount(loadCsv.size());

            If, for whatever reason, a blank line is encountered, say at the end, this would remove all previously-added columns from the table. Do you think this is safe coding?

            D Offline
            D Offline
            Davide00
            wrote on last edited by
            #6

            @JonB I've modified my code and used, now it looks like this:

            void table_dialog::fill_table() {
                QFile file("utenti.csv");
                if(!file.open(QFile::ReadOnly | QFile::Text)) {
                    return;
                }
                QTextStream in(&file);
                int cter = 0;
            
                while(in.atEnd()) {
            
                    QString readedLine = in.readLine();
                    QStringList listValue = readedLine.split(",");
                    cter += 1;
                    for(int i = 0; i < listValue.size(); ++i) {
                        ui->tableWidget->setItem(cter-1, i, new QTableWidgetItem(listValue[i]));
                    }
                }
                file.flush();
                file.close();
            }
            

            I used some qDebug too, and i noticed that readLine() and .split(",") are doing nothing (now i changed the delimeter from "\t" to ",").
            What could possibly be the problem?

            M 1 Reply Last reply
            0
            • M Offline
              M Offline
              mchinand
              wrote on last edited by
              #7

              @Davide00 said in CSV to QTaleWidget:

              I used some qDebug too, and i noticed that readLine() and .split(",") are doing nothing (now i changed the delimeter from "\t" to ",").

              Is your while loop not executed for each line in your file anymore? Are you setting the row and column count of your table anywhere? What do you mean readLine() and split() do nothing? What is the output of each? If you add qDebug() statements, include them in the code you paste here and what does the debugging output show. Also, can you also show us the first few rows of your csv file?

              D 1 Reply Last reply
              0
              • D Davide00

                @JonB I've modified my code and used, now it looks like this:

                void table_dialog::fill_table() {
                    QFile file("utenti.csv");
                    if(!file.open(QFile::ReadOnly | QFile::Text)) {
                        return;
                    }
                    QTextStream in(&file);
                    int cter = 0;
                
                    while(in.atEnd()) {
                
                        QString readedLine = in.readLine();
                        QStringList listValue = readedLine.split(",");
                        cter += 1;
                        for(int i = 0; i < listValue.size(); ++i) {
                            ui->tableWidget->setItem(cter-1, i, new QTableWidgetItem(listValue[i]));
                        }
                    }
                    file.flush();
                    file.close();
                }
                

                I used some qDebug too, and i noticed that readLine() and .split(",") are doing nothing (now i changed the delimeter from "\t" to ",").
                What could possibly be the problem?

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #8

                @Davide00 said in CSV to QTaleWidget:

                while(in.atEnd())

                NOT atEnd() ;)

                1 Reply Last reply
                4
                • M mchinand

                  @Davide00 said in CSV to QTaleWidget:

                  I used some qDebug too, and i noticed that readLine() and .split(",") are doing nothing (now i changed the delimeter from "\t" to ",").

                  Is your while loop not executed for each line in your file anymore? Are you setting the row and column count of your table anywhere? What do you mean readLine() and split() do nothing? What is the output of each? If you add qDebug() statements, include them in the code you paste here and what does the debugging output show. Also, can you also show us the first few rows of your csv file?

                  D Offline
                  D Offline
                  Davide00
                  wrote on last edited by
                  #9

                  @mchinand the code is the same as before, i just corrected the while condition into while(!in.atEnd())
                  I added a qDebug() like this

                  QString readedLine = in.readLine();
                  qDebug() << readedLine;
                  QStringList listValue = readedLine.split(",");
                  qDebug() << listValue
                  

                  and i obtain the following:

                  20:24:10: Starting /home/LABORATORI/dc858025/login_form/login_form/login_form ...
                  "Wonder,Woman,3562145969,abcd,mer dic 25 1963,donna"
                  ("Wonder", "Woman", "3562145969", "abcd", "mer dic 25 1963", "donna")
                  "Bruce,Wayne,imbatman@gmail.com,TheJoker27,mar mag 6 1975,uomo"
                  ("Bruce", "Wayne", "imbatman@gmail.com", "TheJoker27", "mar mag 6 1975", "uomo")
                  20:24:24: /home/LABORATORI/dc858025/login_form/login_form/login_form exited with code 0
                  

                  which is right considering that the csv file is exactly mae up of this two lines

                  Wonder | Woman | 3562145969         | abcd       | mer dic 25 1963  |  donna
                  Bruce  | Wayne | imbatman@gmail.com | TheJoker27 | mar mag 6 1975   |  uomo
                  

                  the problem is that the QTableWidget is still empty, and i don't know why

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    You don't set a row and column count so what do you expect?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    D M 2 Replies Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      You don't set a row and column count so what do you expect?

                      D Offline
                      D Offline
                      Davide00
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher I've followed an half semester course on c++ and only the last three lessons were on Qt, so I basically know nothing.
                      How can i set a row and column?

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        You don't set a row and column count so what do you expect?

                        M Offline
                        M Offline
                        mpergand
                        wrote on last edited by mpergand
                        #12

                        @Christian-Ehrlicher said in CSV to QTaleWidget:

                        You don't set a row and column count so what do you expect?

                        I've never used QTableWidget, but I thought rows and columns were created automatically as GridLayout do.
                        Disappointed i am :)

                        1 Reply Last reply
                        0
                        • D Davide00

                          @Christian-Ehrlicher I've followed an half semester course on c++ and only the last three lessons were on Qt, so I basically know nothing.
                          How can i set a row and column?

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @Davide00 said in CSV to QTaleWidget:

                          How can i set a row and column?

                          By clicking on my links and reading the documentation.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          1
                          • D Offline
                            D Offline
                            Davide00
                            wrote on last edited by
                            #14

                            So something like this

                            void table_dialog::fill_table() {
                                QFile file("utenti.csv");
                                if(!file.open(QFile::ReadOnly | QFile::Text)) {
                                    return;
                                }
                                QTextStream in(&file);
                                int cter = 0;
                                QString readedLine = in.readLine();
                                QStringList listValue = readedLine.split(",");
                                ui->tableWidget->setColumnCount(listValue().size());
                            
                                while(!in.atEnd()) {
                            
                                    QString readedLine = in.readLine();
                                    QStringList listValue = readedLine.split(",");
                                    cter += 1;
                                    ui->tableWidget->setRowCount(cter);
                                    for(int i = 0; i < listValue.size(); ++i) {
                                        ui->tableWidget->setItem(cter-1, i, new QTableWidgetItem(listValue[i]));
                                    }
                                }
                                file.flush();
                                file.close();
                            }
                            

                            should do the work?

                            D 1 Reply Last reply
                            0
                            • D Davide00

                              So something like this

                              void table_dialog::fill_table() {
                                  QFile file("utenti.csv");
                                  if(!file.open(QFile::ReadOnly | QFile::Text)) {
                                      return;
                                  }
                                  QTextStream in(&file);
                                  int cter = 0;
                                  QString readedLine = in.readLine();
                                  QStringList listValue = readedLine.split(",");
                                  ui->tableWidget->setColumnCount(listValue().size());
                              
                                  while(!in.atEnd()) {
                              
                                      QString readedLine = in.readLine();
                                      QStringList listValue = readedLine.split(",");
                                      cter += 1;
                                      ui->tableWidget->setRowCount(cter);
                                      for(int i = 0; i < listValue.size(); ++i) {
                                          ui->tableWidget->setItem(cter-1, i, new QTableWidgetItem(listValue[i]));
                                      }
                                  }
                                  file.flush();
                                  file.close();
                              }
                              

                              should do the work?

                              D Offline
                              D Offline
                              Davide00
                              wrote on last edited by
                              #15

                              @Davide00 However doing in this way, it only shows me the second row of elements

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mchinand
                                wrote on last edited by
                                #16

                                You're reading the first line of your file in your three lines before the while loop. You could add a header to your csv file, so the header is read first and then all your data rows are read within the while loop. Alternatively, remove that readline() before the while loop and add this within loop:

                                if (listValue.size() > ui->tableWidget->columnCount()){
                                   ui->tableWidget->setColumnCount(listValue.size());
                                }
                                D 1 Reply Last reply
                                1
                                • M mchinand

                                  You're reading the first line of your file in your three lines before the while loop. You could add a header to your csv file, so the header is read first and then all your data rows are read within the while loop. Alternatively, remove that readline() before the while loop and add this within loop:

                                  if (listValue.size() > ui->tableWidget->columnCount()){
                                     ui->tableWidget->setColumnCount(listValue.size());
                                  }
                                  D Offline
                                  D Offline
                                  Davide00
                                  wrote on last edited by
                                  #17

                                  @mchinand You've saved me, thank you very much

                                  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 Extensions
                                  • Unsolved