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. Application not responding when export huge data to CSV.
Forum Updated to NodeBB v4.3 + New Features

Application not responding when export huge data to CSV.

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 1.3k 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.
  • mangekoyuM Offline
    mangekoyuM Offline
    mangekoyu
    wrote on last edited by
    #1

    Hello. I have a function for export data to csv. It's working well but when I try to export huge data like 100K. application not responding. I have no idea why I got this error. Can someone helo me please?

    Here is export function.

    void MainWindow::exportArraysToCSV(QList < QList < double >> dataColums) {
      QString filters("CSV files (*.csv);;All files (*.*)");
      QString defaultFilter("CSVi files (*.csv)");
      QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
        filters, & defaultFilter);
      QFile file(fileName);
    
      if (file.open(QFile::WriteOnly | QFile::Append)) {
        QTextStream data( & file);
        QStringList strList;
        QString formula;
    
    
    
      data << strList.join(",") << "\n";
    
        int maxRowCount = 0;
        foreach(auto column, dataColums)
        maxRowCount = qMax(maxRowCount, column.count());
    
        for (int i = 5; i < maxRowCount; ++i) // rows
        {
    
          strList.clear();
          for (int j = 0; j < 2; ++j) // columns
          {
    
    
            if (i < dataColums[j].count()){
              
                strList.append(QString::number(dataColums[j][i], 'f'));
                
            }
    
    
            else
              strList.append("\"\" ");
          }
    
          data << strList.join(";") + "\n";
    
    
        }
    
        file.close();
      }
    }
    
    JonBJ 1 Reply Last reply
    0
    • mangekoyuM mangekoyu

      Hello. I have a function for export data to csv. It's working well but when I try to export huge data like 100K. application not responding. I have no idea why I got this error. Can someone helo me please?

      Here is export function.

      void MainWindow::exportArraysToCSV(QList < QList < double >> dataColums) {
        QString filters("CSV files (*.csv);;All files (*.*)");
        QString defaultFilter("CSVi files (*.csv)");
        QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
          filters, & defaultFilter);
        QFile file(fileName);
      
        if (file.open(QFile::WriteOnly | QFile::Append)) {
          QTextStream data( & file);
          QStringList strList;
          QString formula;
      
      
      
        data << strList.join(",") << "\n";
      
          int maxRowCount = 0;
          foreach(auto column, dataColums)
          maxRowCount = qMax(maxRowCount, column.count());
      
          for (int i = 5; i < maxRowCount; ++i) // rows
          {
      
            strList.clear();
            for (int j = 0; j < 2; ++j) // columns
            {
      
      
              if (i < dataColums[j].count()){
                
                  strList.append(QString::number(dataColums[j][i], 'f'));
                  
              }
      
      
              else
                strList.append("\"\" ");
            }
      
            data << strList.join(";") + "\n";
      
      
          }
      
          file.close();
        }
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @mangekoyu
      What "error"? It presumably takes this long to build the lists and output them to file. During that time your UI will be blocked, unless you do a bit at a time on a timer or in a separate, non-UI thread.

      piervalliP 1 Reply Last reply
      3
      • mangekoyuM Offline
        mangekoyuM Offline
        mangekoyu
        wrote on last edited by
        #3

        @JonB said in Application not responding when export huge data to CSV.:

        UI thread.

        I'm waited around 1 hour but st'll saying program not responding. How can I meke it like you said?
        My problem ıs it's taking long time to export and user can say Oh program crashed.

        JonBJ 1 Reply Last reply
        0
        • mangekoyuM mangekoyu

          @JonB said in Application not responding when export huge data to CSV.:

          UI thread.

          I'm waited around 1 hour but st'll saying program not responding. How can I meke it like you said?
          My problem ıs it's taking long time to export and user can say Oh program crashed.

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

          @mangekoyu
          It should not take an hour! :)

          I cannot see a fault in that code which would cause it to loop indefinitely. Why don't you put some qDebug() statements in so you can see where it is getting, or step through it in debugger?

          mangekoyuM 1 Reply Last reply
          0
          • JonBJ JonB

            @mangekoyu
            It should not take an hour! :)

            I cannot see a fault in that code which would cause it to loop indefinitely. Why don't you put some qDebug() statements in so you can see where it is getting, or step through it in debugger?

            mangekoyuM Offline
            mangekoyuM Offline
            mangekoyu
            wrote on last edited by
            #5

            @JonB I tried put qDebug() to all lines in function but when application says not responding qDebug() I cannot see any debug :(

            JonBJ 1 Reply Last reply
            0
            • mangekoyuM mangekoyu

              @JonB I tried put qDebug() to all lines in function but when application says not responding qDebug() I cannot see any debug :(

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

              @mangekoyu
              What platform are you on? Windows you (apparently) have to take special action to see debug messages?

              But can't you see them in Creator-debugger? Are you even sure you hit this function? Why don't you step through it in debugger?

              1 Reply Last reply
              1
              • JonBJ JonB

                @mangekoyu
                What "error"? It presumably takes this long to build the lists and output them to file. During that time your UI will be blocked, unless you do a bit at a time on a timer or in a separate, non-UI thread.

                piervalliP Offline
                piervalliP Offline
                piervalli
                wrote on last edited by piervalli
                #7

                @JonB You can not block UI than gran of 1m , you should move on secondary thread with qconcurrency

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

                  Hi,

                  @mangekoyu said in Application not responding when export huge data to CSV.:

                  maxRowCount = qMax(maxRowCount, column.count());

                  Why is your max row count tied to the column count ?

                  What does 100k represent ?
                  How many columns do you have ?

                  Your logic is also not clear, you have some magic numbers here and there which makes it hard to really follow what should go into your 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

                  JonBJ 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    @mangekoyu said in Application not responding when export huge data to CSV.:

                    maxRowCount = qMax(maxRowCount, column.count());

                    Why is your max row count tied to the column count ?

                    What does 100k represent ?
                    How many columns do you have ?

                    Your logic is also not clear, you have some magic numbers here and there which makes it hard to really follow what should go into your CSV file.

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

                    @SGaist said in Application not responding when export huge data to CSV.:

                    Why is your max row count tied to the column count ?

                    Instead of the normal, with rows having columns, and maybe not all rows having the same number of columns, the OP seems to have a list of columns which hold the rows.... The mac row count seems to be the max rows among all columns....

                    1 Reply Last reply
                    0
                    • mangekoyuM Offline
                      mangekoyuM Offline
                      mangekoyu
                      wrote on last edited by
                      #10

                      @SGaistThere will be 2 columns and
                      each will have a maximum of 170,000 rows.

                      JonBJ 1 Reply Last reply
                      0
                      • mangekoyuM mangekoyu

                        @SGaistThere will be 2 columns and
                        each will have a maximum of 170,000 rows.

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

                        @mangekoyu
                        That is a lot of rows, which will take a bit of time, but still not 1 hour/hangs/crashes. Like I said, put in a qDebug() or step through to see what is happening.

                        Separate points:

                        • data << strList.join(",") << "\n";: this always just puts a single \n, nothing else, into data. What's the point?
                        • data << strList.join(";") + "\n";: this outputs one line with the columns joined by ;. Normally CSV files use , as the column joiner/separator, hope you know what you are doing. [ @J-Hilk has explained to me that CSVs use locale numbers.]
                        J.HilkJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @mangekoyu
                          That is a lot of rows, which will take a bit of time, but still not 1 hour/hangs/crashes. Like I said, put in a qDebug() or step through to see what is happening.

                          Separate points:

                          • data << strList.join(",") << "\n";: this always just puts a single \n, nothing else, into data. What's the point?
                          • data << strList.join(";") + "\n";: this outputs one line with the columns joined by ;. Normally CSV files use , as the column joiner/separator, hope you know what you are doing. [ @J-Hilk has explained to me that CSVs use locale numbers.]
                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @JonB said in Application not responding when export huge data to CSV.:

                          Normally CSV files use , as the column joiner/separator, hope you know what you are doing.

                          in the English speaking world maybe, over here, its ; as default separator because , is usually the "decimal point"


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          JonBJ 1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @JonB said in Application not responding when export huge data to CSV.:

                            Normally CSV files use , as the column joiner/separator, hope you know what you are doing.

                            in the English speaking world maybe, over here, its ; as default separator because , is usually the "decimal point"

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

                            @J-Hilk
                            Ohhhh, I didn't know that! [I will correct that point in my answer.] In that case, CSV files differ by locale, but don't say which locale they use, so goodness knows how it works across countries!

                            UPDATE
                            Does this apply to CSV files though? [Wikipedia states](https://en.wikipedia.org/wiki/Comma-separated_values#:~:text=A comma-separated values (CSV,name for this file format.):

                            A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.

                            Alternative delimiter-separated files are often given a ".csv" extension despite the use of a non-comma field separator. This loose terminology can cause problems in data exchange.

                            Hmm. "This loose terminology can cause problems in data exchange" :)

                            J.HilkJ 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @J-Hilk
                              Ohhhh, I didn't know that! [I will correct that point in my answer.] In that case, CSV files differ by locale, but don't say which locale they use, so goodness knows how it works across countries!

                              UPDATE
                              Does this apply to CSV files though? [Wikipedia states](https://en.wikipedia.org/wiki/Comma-separated_values#:~:text=A comma-separated values (CSV,name for this file format.):

                              A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.

                              Alternative delimiter-separated files are often given a ".csv" extension despite the use of a non-comma field separator. This loose terminology can cause problems in data exchange.

                              Hmm. "This loose terminology can cause problems in data exchange" :)

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              @JonB are you sure ? I have a really strong déjà vu here!

                              @mangekoyu
                              print the maxRowCount after you finished calculating it. See how big this actually is!

                              foreach(auto column, dataColums)
                                   maxRowCount = qMax(maxRowCount, column.count());
                              qDebug() << maxRowCount;
                              

                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              mangekoyuM 1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @JonB are you sure ? I have a really strong déjà vu here!

                                @mangekoyu
                                print the maxRowCount after you finished calculating it. See how big this actually is!

                                foreach(auto column, dataColums)
                                     maxRowCount = qMax(maxRowCount, column.count());
                                qDebug() << maxRowCount;
                                
                                mangekoyuM Offline
                                mangekoyuM Offline
                                mangekoyu
                                wrote on last edited by mangekoyu
                                #15

                                @J-Hilk @JonB It works as you said. I must wait untill finish write all rows and columns. I think I can put progressbar for make user wait. Thank you for all answers.

                                J.HilkJ 1 Reply Last reply
                                0
                                • mangekoyuM mangekoyu

                                  @J-Hilk @JonB It works as you said. I must wait untill finish write all rows and columns. I think I can put progressbar for make user wait. Thank you for all answers.

                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  @mangekoyu there should be ways to make this faster,

                                  • don't use join and not int he frequency you do, it is a heavy operation, and you actually don't need it
                                  • I don't know what dataColums type is, but if its a QtContainer, consider using at() for fetching read only const reference, it should be faster
                                  • drop strList all together, you have a QTextStream operator, that one excepts nearly everything, especially integers, so no need to call QString::Number either
                                    if (file.open(QFile::WriteOnly | QFile::Append)) {
                                      QTextStream data( & file);
                                  
                                      data << QChar::LineFeed;
                                  
                                      int maxRowCount = 0;
                                      foreach(auto column, dataColums)
                                          maxRowCount = qMax(maxRowCount, column.count());
                                  
                                      for (int row = 5; row < maxRowCount; ++i) {
                                         for (int col = 0; col < 2; ++j) {
                                              if (row < dataColums.at(col).count()) {
                                                  data << dataColums[j][i];
                                              } else {
                                                 data << QChar('"') << QChar('"');
                                              }
                                          }
                                          data << QChar(';') << QChar::LineFeed;
                                      }
                                  
                                      file.close();
                                    }
                                  
                                  

                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  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