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. Import .CSV data to QTableView
Forum Updated to NodeBB v4.3 + New Features

Import .CSV data to QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 9.8k 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.
  • ro12man3R Offline
    ro12man3R Offline
    ro12man3
    wrote on last edited by ro12man3
    #1

    Hi! I wanna read data from .csv file into QTableView.
    I used this code, but it doesn't work. How to fix that? Please help

    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();
    }
    ui->tableView->setModel(model);```// It doesn't work
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ro12man3 said:

      hi , does it read the data from the file?

      You should check if any in list

      qDebug << "fields:" << lineToken.size();

      ro12man3R 1 Reply Last reply
      0
      • mrjjM mrjj

        @ro12man3 said:

        hi , does it read the data from the file?

        You should check if any in list

        qDebug << "fields:" << lineToken.size();

        ro12man3R Offline
        ro12man3R Offline
        ro12man3
        wrote on last edited by
        #3

        @mrjj no. It doesn't. I was found that solution here http://stackoverflow.com/questions/27354200/uploading-csv-or-txt-file-to-populate-qtableview .
        But I don't know how to integrate this with qtableview

        mrjjM 1 Reply Last reply
        0
        • ro12man3R ro12man3

          @mrjj no. It doesn't. I was found that solution here http://stackoverflow.com/questions/27354200/uploading-csv-or-txt-file-to-populate-qtableview .
          But I don't know how to integrate this with qtableview

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @ro12man3
          Well code looks fine :)
          So if u dont get any in list, it means file empty or wrong format.

          can u post the first 10 lines from file u read in?

          also test with

          qDebug() << "reading:" << fileLine ;

          ro12man3R 1 Reply Last reply
          0
          • mrjjM mrjj

            @ro12man3
            Well code looks fine :)
            So if u dont get any in list, it means file empty or wrong format.

            can u post the first 10 lines from file u read in?

            also test with

            qDebug() << "reading:" << fileLine ;

            ro12man3R Offline
            ro12man3R Offline
            ro12man3
            wrote on last edited by
            #5

            @mrjj well, it still doesn't work.
            The data in .csv file is very simple:
            2 4 6
            1 3 5

            jsulmJ 1 Reply Last reply
            1
            • ro12man3R ro12man3

              @mrjj well, it still doesn't work.
              The data in .csv file is very simple:
              2 4 6
              1 3 5

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

              @ro12man3 Take a look at this line:

              QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts); 
              

              it is splitting on ',' character not blanks!
              Change it to:

              QStringList lineToken = fileLine.split(' ', QString::SkipEmptyParts); 
              

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

              ro12man3R 1 Reply Last reply
              3
              • jsulmJ jsulm

                @ro12man3 Take a look at this line:

                QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts); 
                

                it is splitting on ',' character not blanks!
                Change it to:

                QStringList lineToken = fileLine.split(' ', QString::SkipEmptyParts); 
                
                ro12man3R Offline
                ro12man3R Offline
                ro12man3
                wrote on last edited by
                #7

                @jsulm I changed on that

                 QStringList lineToken = fileLine.split(";", QString::SkipEmptyParts);
                

                and now it's working correctly

                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