Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. I'm trying to call a C++ function from qml to read a csv and enter that data into sql table, but it freezes the UI.
QtWS25 Last Chance

I'm trying to call a C++ function from qml to read a csv and enter that data into sql table, but it freezes the UI.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 4 Posters 2.0k Views
  • 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.
  • J Offline
    J Offline
    Jimit Rupani
    wrote on last edited by
    #1

    Whenever i am reading files from pen drive and write into the database it is taking so much time and freezes the GUI. I am using Qobject.

    class ReportModel : public QObject
    

    i have like 99 program and 99 steps in an individual program so when i am reading individual files from the pen drive and inserting its value in to database from qml

    qml code

    var success = report.importProgram(listArray[i])
                            if(!success)
                                    /something
    importRemainingArray.splice(importRemainingArray.indexOf(listArray[i],1))
    

    cpp code

    QString fileData = csvFile.readAll();
    ProgramModel *program = new ProgramModel();
    query.prepare("INSERT INTO Programs");
    
    //inserting into step tabel of an individual prog
    
    QStringList list = fileData.split("\n");
     for(int i=1;i<list.length();i++)
            {
                StepModel *step = new StepModel();
     query.prepare("INSERT INTO Steps(programId)");
      query.bindValue(":programId",step->programId());
    }
    csvFile.close();
    

    this Sql query part is causing the freezing UI part because i am calling on an individual element from listArray and passing it to c++. so how can i solve this problem?

    JonBJ 1 Reply Last reply
    0
    • J Jimit Rupani

      Whenever i am reading files from pen drive and write into the database it is taking so much time and freezes the GUI. I am using Qobject.

      class ReportModel : public QObject
      

      i have like 99 program and 99 steps in an individual program so when i am reading individual files from the pen drive and inserting its value in to database from qml

      qml code

      var success = report.importProgram(listArray[i])
                              if(!success)
                                      /something
      importRemainingArray.splice(importRemainingArray.indexOf(listArray[i],1))
      

      cpp code

      QString fileData = csvFile.readAll();
      ProgramModel *program = new ProgramModel();
      query.prepare("INSERT INTO Programs");
      
      //inserting into step tabel of an individual prog
      
      QStringList list = fileData.split("\n");
       for(int i=1;i<list.length();i++)
              {
                  StepModel *step = new StepModel();
       query.prepare("INSERT INTO Steps(programId)");
        query.bindValue(":programId",step->programId());
      }
      csvFile.close();
      

      this Sql query part is causing the freezing UI part because i am calling on an individual element from listArray and passing it to c++. so how can i solve this problem?

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

      @Jimit-Rupani
      If you are saying you are reading lots of individual files in(?), you should be putting a "process events" call to allow UI responsiveness in between each file open.

      You may get somewhere with that if you put it in the middle of reading each file. But USB sticks are so slow at reading you may not. I would have thought most of your "dead" rime is during csvFile.readAll();, though it might spend considerable time opening it too.

      The SQL part should only take a long time if it's a big file. Like I wrote, reading from stick almost certainly takes longer.

      an individual element from listArray and passing it to c++

      I don't know what this is about, but I doubt it's any fraction of a second compared to what I have said above.

      J 1 Reply Last reply
      0
      • JonBJ JonB

        @Jimit-Rupani
        If you are saying you are reading lots of individual files in(?), you should be putting a "process events" call to allow UI responsiveness in between each file open.

        You may get somewhere with that if you put it in the middle of reading each file. But USB sticks are so slow at reading you may not. I would have thought most of your "dead" rime is during csvFile.readAll();, though it might spend considerable time opening it too.

        The SQL part should only take a long time if it's a big file. Like I wrote, reading from stick almost certainly takes longer.

        an individual element from listArray and passing it to c++

        I don't know what this is about, but I doubt it's any fraction of a second compared to what I have said above.

        J Offline
        J Offline
        Jimit Rupani
        wrote on last edited by
        #3

        @JonB
        an individual element from listArray and passing it to c++ is because of i have 99 prog and each and everyone have more 99 steps based on that program id. i will look for process events. thanks for the suggestion.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jimit Rupani
          wrote on last edited by Jimit Rupani
          #4

          I have tried this with the thread as well but its still freezing the UI.

          QThread to send large queries to the database

          JonBJ 1 Reply Last reply
          0
          • J Jimit Rupani

            I have tried this with the thread as well but its still freezing the UI.

            QThread to send large queries to the database

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

            @Jimit-Rupani
            I have suggested to you that the time "freezing" is presumably when reading the CSV "files from pen drive". Therefore your "QThread to send large queries to the database" will not help. Look at reading the file from an external device, rather than sending to SQL, as your issue.

            1 Reply Last reply
            1
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              UI freeze means that you are doing time consuming work in UI thread. Just try one thing. When you request the data from C++ object, don't read from USB/CSV. Just return dummy values from C++ object and see if still UI freezes. It should not freeze.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              J 1 Reply Last reply
              0
              • dheerendraD dheerendra

                UI freeze means that you are doing time consuming work in UI thread. Just try one thing. When you request the data from C++ object, don't read from USB/CSV. Just return dummy values from C++ object and see if still UI freezes. It should not freeze.

                J Offline
                J Offline
                Jimit Rupani
                wrote on last edited by
                #7

                @dheerendra Of course if i will return dummy value like that then it won't freeze the ui because it will not go into query thing.

                JonBJ 1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  This itself indicates that your query is culprit for UI freeze. This time consuming task need to be moved to worker thread. Use signals/slots to exchange the data between main thread(QML) and worker thread.

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  1
                  • J Jimit Rupani

                    @dheerendra Of course if i will return dummy value like that then it won't freeze the ui because it will not go into query thing.

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

                    @Jimit-Rupani
                    For the record:

                    • Your code as shown inserts nothing into the database. Unless there is more code that you are not showing us.
                    • You talk about "a SQL database", but do not say what SQL database you are using.

                    both of which will affect any possible solution.

                    1 Reply Last reply
                    0
                    • dheerendraD Offline
                      dheerendraD Offline
                      dheerendra
                      Qt Champions 2022
                      wrote on last edited by
                      #10

                      Do you still have the issue or is it resolved ?

                      Dheerendra
                      @Community Service
                      Certified Qt Specialist
                      http://www.pthinks.com

                      J 1 Reply Last reply
                      0
                      • dheerendraD dheerendra

                        Do you still have the issue or is it resolved ?

                        J Offline
                        J Offline
                        Jimit Rupani
                        wrote on last edited by
                        #11

                        @dheerendra resolved. Sorry for the late reply.

                        Pablo J. RoginaP 1 Reply Last reply
                        0
                        • J Jimit Rupani

                          @dheerendra resolved. Sorry for the late reply.

                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on last edited by
                          #12

                          @Jimit-Rupani said in I'm trying to call a C++ function from qml to read a csv and enter that data into sql table, but it freezes the UI.:

                          resolved

                          so please don't forget to mark your post as solved!

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          0
                          • J Jimit Rupani has marked this topic as solved on

                          • Login

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