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. Qtableview open csv file
Forum Updated to NodeBB v4.3 + New Features

Qtableview open csv file

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 704 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.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by
    #1

    Hi,
    I am using qtableview and using action save , saving in a csv file, but when i'm opening the same file then its also adding header in the table, that i don't want,

    also when I open csv file then the color is not coming ( adding color using a diff function, for speific columns using Qbrush), for rows which are filling through csv

    void table_inpiut::action_Open() {
    QString filters("CSV files (*.csv);");
    QString fileName = QFileDialog::getOpenFileName(this, "Open file", QDir::homePath(), filters);
    QFile file(fileName);
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(ui.tableView->model());
     if (file.open(QIODevice::ReadOnly)){
     //  QMessageBox::information(0, "Error!", "Error opening file!", 0);
       int lineindex = 0;                     // file line counter
       QTextStream in_file(&file);                 // read to text stream     
       while (!in_file.atEnd()) {
           // read one line from textstream(separated by "\n") 
           QString fileLine = in_file.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();
    }
    }
    

    what need to change in this code?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      n-2204
      wrote on last edited by n-2204
      #6

      I just called colorcolumn() fun inside openfile();
      color is coming now
      and for ignore 1row before while loop i added in_file.readLine()

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        The header is usually the first line of the file so just ignore it.

        As for the colors, are you storing them in the CSV file ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        N 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          The header is usually the first line of the file so just ignore it.

          As for the colors, are you storing them in the CSV file ?

          N Offline
          N Offline
          n-2204
          wrote on last edited by
          #3

          @SGaist // read one line from textstream(separated by "\n")
          QString fileLine = in_file.readLine();
          in this i should ignore first line, like -1 something or any func. is there ?

          No color I'm not storing in csv / but it can be case user import somefile which is not saved in this project.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Either do one readLine before your while loop or use a Boolean to skip processing the first line you read in your while loop.

            As for your color issue, how do you apply it ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            1
            • SGaistS SGaist

              Either do one readLine before your while loop or use a Boolean to skip processing the first line you read in your while loop.

              As for your color issue, how do you apply it ?

              N Offline
              N Offline
              n-2204
              wrote on last edited by
              #5

              @SGaist
              ok i'll try this readline
              color columns->

               QAbstractItemModel* abstrctitem_table1 = ui.tableView->model();
              for (int irowTable1 = 0; irowTable1 < abstrctitem_table1->rowCount(); ++irowTable1) {
                  for (int columnTable1 : {0, 1, 2, 3, 4, 5, 8})
                      abstrctitem_table1->setData(abstrctitem_table1->index(irowTable1, columnTable1), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
              }
              QObject::connect(abstrctitem_table1, &QAbstractItemModel::rowsInserted,
                  abstrctitem_table1, [abstrctitem_table1](const QModelIndex& parent, int first, int last) {
                      for (int irowTable1 = first; irowTable1 <= last;++irowTable1) {
                          for (int columnTable1 : {0, 1, 2, 3, 4, 5, 8})
                              abstrctitem_table1->setData(abstrctitem_table1->index(irowTable1, columnTable1, parent), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
                      }
                  });
              
              1 Reply Last reply
              0
              • N Offline
                N Offline
                n-2204
                wrote on last edited by n-2204
                #6

                I just called colorcolumn() fun inside openfile();
                color is coming now
                and for ignore 1row before while loop i added in_file.readLine()

                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