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. Save QtableData in csv file

Save QtableData in csv file

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.8k 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.
  • A Offline
    A Offline
    Abhi_oct30
    wrote on last edited by koahnig
    #1

    I am facing a minor issue while saving data from QStandatdItemViewModel of TableView in a text file seperated by comma and newline.
    The problem is if i dont update the model then the data is stored properly. But if i update any filed in the tableview the newline is skipped.

    Please help.

    void MainWindow::mSaveTextFile()
    {
        // [Collect model data to QString]
        QString textData;
        int rows = pModel->rowCount();
        int columns = pModel->columnCount();
        qDebug() << rows<<"  "<<columns;
    
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                    textData += pModel->data(pModel->index(i,j)).toString();
                    qDebug() <<textData<<endl;
                    if(j == 0)
                        textData += ", " ;     // for .csv file format
            }
            textData += "\n";             // ( for new line segmentation)
        }
    
        // [Save to file] (header file <QFile> needed)
        // .txt
        QFile txtFile(sSaveFile);
        if(txtFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
    
            QTextStream out(&txtFile);
            out << textData;
    
            txtFile.close();
        }
    }
    
    K 1 Reply Last reply
    0
    • A Abhi_oct30

      I am facing a minor issue while saving data from QStandatdItemViewModel of TableView in a text file seperated by comma and newline.
      The problem is if i dont update the model then the data is stored properly. But if i update any filed in the tableview the newline is skipped.

      Please help.

      void MainWindow::mSaveTextFile()
      {
          // [Collect model data to QString]
          QString textData;
          int rows = pModel->rowCount();
          int columns = pModel->columnCount();
          qDebug() << rows<<"  "<<columns;
      
          for (int i = 0; i < rows; i++)
          {
              for (int j = 0; j < columns; j++)
              {
                      textData += pModel->data(pModel->index(i,j)).toString();
                      qDebug() <<textData<<endl;
                      if(j == 0)
                          textData += ", " ;     // for .csv file format
              }
              textData += "\n";             // ( for new line segmentation)
          }
      
          // [Save to file] (header file <QFile> needed)
          // .txt
          QFile txtFile(sSaveFile);
          if(txtFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
      
              QTextStream out(&txtFile);
              out << textData;
      
              txtFile.close();
          }
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Abhi_oct30

      I have added source code markers with the right icon above the edit window.

      Your source looks for the newline character ok to me.

      Odd is the check with j==0. This will work only for 2 columns. For other cols the output will be skipped.

      I would try to use the debugger with some break points in the loop. Then you can check step by step.

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

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Abhi_oct30
        wrote on last edited by Abhi_oct30
        #3

        my data is having 2 columns so my intention is to add " , " only after 1 column data. After 2nd column I want new line to be added.For this j==0 check is made.
        Expected data:
        Adam, 20
        Charles, 40
        Delta-F , 70
        Falcon, 35

        Observed Output:
        Adam, 20
        Charles, 40Delta-F , 70
        Falcon, 35

        This occurs only if i edit the field with value 40.

        K 1 Reply Last reply
        0
        • A Abhi_oct30

          my data is having 2 columns so my intention is to add " , " only after 1 column data. After 2nd column I want new line to be added.For this j==0 check is made.
          Expected data:
          Adam, 20
          Charles, 40
          Delta-F , 70
          Falcon, 35

          Observed Output:
          Adam, 20
          Charles, 40Delta-F , 70
          Falcon, 35

          This occurs only if i edit the field with value 40.

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @Abhi_oct30

          Do you see this output in the output file or with qDebug() you are using?

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

          A 1 Reply Last reply
          0
          • K koahnig

            @Abhi_oct30

            Do you see this output in the output file or with qDebug() you are using?

            A Offline
            A Offline
            Abhi_oct30
            wrote on last edited by
            #5

            @koahnig output file.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              You are saving in binary format rather than text that's what's causing your problem (i.e. you have to open the file with the QIODevice::Text flag). you also have to escape special characters in the fields (what happens if there is a , or a " in the first column?)

              I have implemented this code: https://github.com/VSRonin/Qt-Model-Serialisation/tree/dev the documentation is not yet complete (hence it not being merged in the master branch) but the example should give you a stating point and if you struggle I'm happy to help

              CsvModelSerialiser csvSave(pModel);
              QFile tempFile("TestSave.csv");
              if(!csvSave.saveModel(&tempFile)) Q_ASSERT(false);
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2

              • Login

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