Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Need help on QtConcurrent usage.

    General and Desktop
    3
    6
    1852
    Loading More Posts
    • 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.
    • E
      ercyd last edited by

      Hi to all,
      I'm doing some heavy computing, like accessing a database, that blocks my UI. So, I'm implementing the QtConcurrent::run function to start some class methods. These methods retrieve data from database table to fill out a classic QComboBox. After the
      @ui->setupUi(this);@

      in my form ctor I'm running these commands:

      @
      QFuture<void> f1 = QtConcurrent::run(this, &frmPractices_CRUD::populateComboSubjects);
      QFuture<void> f2 = QtConcurrent::run(this, &frmPractices_CRUD::populateComboSellPoints);
      f1.waitForFinished();
      f2.waitForFinished();
      @

      and the code is compiled but I get a Visual C++ Runtime Library errors!
      Then I tried to debug the functions and maybe my debugging skill isn't so good yet. I get the program works without errors only making the following changes:

      @
      QFuture<void> f1 = QtConcurrent::run(this, &frmPractices_CRUD::populateComboSubjects);
      f1.waitForFinished();
      QFuture<void> f2 = QtConcurrent::run(this, &frmPractices_CRUD::populateComboSellPoints);
      f2.waitForFinished();
      @

      And I don't think that's the right way to use QtConcurrent, my populateComboABCD functions contain the same code:

      @
      ui->cboSubject->clear();

      QSqlDatabase db = QSqlDatabase::database();
      QString tSqlCommand =
          QString(
              "SELECT "
              "sog_id, "
              "concat_ws(' ', sog_cogn, sog_nome) AS names, "
              "concat_ws(' - ', sog_codf, sog_piva) AS fiscals "
              "FROM %1.subjects "
              "WHERE (sog_tipo = 1) OR (sog_tipo = 3) "
              "ORDER BY names"
          ).arg(MyQtUtility::instance()->schemaName());
      QSqlQuery tSqlQuery(db);
      if (tSqlQuery.exec&#40;tSqlCommand&#41;) {
          while (tSqlQuery.next()) {
              QString tmpCboText;
      
              tmpCboText += tSqlQuery.value(1).toString().simplified();
              if (!tSqlQuery.value(2).toString().simplified().isEmpty()) {
                  tmpCboText += " / " + tSqlQuery.value(2).toString().simplified();
              }
      
              ui->cboSubject->addItem(tmpCboText.trimmed(), tSqlQuery.value(0).toInt());
          }
      }
      tSqlQuery.finish();
      

      @

      Thanks in advance,
      Danilo

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        @
        QFuture<void> f1 = QtConcurrent::run(this, &frmPractices_CRUD::populateComboSubjects);
        f1.waitForFinished();
        QFuture<void> f2 = QtConcurrent::run(this, &frmPractices_CRUD::populateComboSellPoints);
        f2.waitForFinished();
        @
        You're right. It's not good this way. This is a sequential code. It is the same as
        @
        populateComboSubjects();
        populateComboSellPoints();
        @
        The problem is not with QtConcurrent. It's with the populate* methods. They run in separate threads and you can access UI only in the main thread, so calls to ui->cboSubject->clear() and ui->cboSubject->addItem() are not allowed there.

        You can run your sql queries in the worker threads but you need to signal the main thread to do the UI update.

        1 Reply Last reply Reply Quote 0
        • E
          ercyd last edited by

          Thanks Chris, but commenting the ui->cboSubject calls I get the same errors. I noted that the debug mode is stopping on MyQtUtility::instance()->schemaName() (it's a singleton class) command whithout breakpoint!! I don't know how strange it is but seems a wrong behavior.

          1 Reply Last reply Reply Quote 0
          • E
            ercyd last edited by

            I googled a bit and got the perception that a QSqlDatabase can not be used within two threads.

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Hi,

              AFAIK, you need to at least create a different connection for each thread in order to access the database.

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

              1 Reply Last reply Reply Quote 0
              • E
                ercyd last edited by

                Thanks Sgaist, I'll investigate that and if the game is worth the candle.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post