Important: Please read the Qt Code of Conduct -

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.

  • 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])

    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)");

    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?

  • @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.

  • @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.

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

    QThread to send large queries to the database

  • @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.

  • Qt Champions 2017

    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 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.

  • Qt Champions 2017

    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.

  • @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.

  • Qt Champions 2017

    Do you still have the issue or is it resolved ?

  • @dheerendra resolved. Sorry for the late reply.

Log in to reply