Qt Forum

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

    Call for Presentations - Qt World Summit

    Unsolved updating db through QThread (movetothread)

    General and Desktop
    4
    7
    1535
    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.
    • R
      RahibeMeryem last edited by

      Hi,

      I try to utilize QThread for object detection besides the main Gui thread . It is working well. So I decide to seperate mongodb update (remote) part to another thread. Beacause its blocking and took 0,3 - ,0,6 sec due to network distance etc.

      this is main gui widget :

      Widget::Widget(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Widget)
      {
          ui->setupUi(this);
          thread = new QThread();
          worker = new Worker();
          worker->moveToThread(thread);
      
      
          mongo = new mongoUpdate();
          mongo->moveToThread(thread_mongo);
      
          thread_mongo->start();
      
          connect(worker, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString)));
          connect(worker, SIGNAL(frameChanged(QImage)), this , SLOT(displayFrame(QImage)));
      //    connect(worker, SIGNAL(liveFrameChanged(QImage)),this , SLOT(liveChanged(QImage)));
      
          connect(worker , SIGNAL(_bigChip_changed(QImage)),this , SLOT(bigChip_update(QImage)));
      
          connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
          connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
          connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
          connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
      

      So I want to start mongoupdate thread to wait my command for a seperate proecess.

      in mongo.cpp:

      #include "mongoupdate.h"
      #include <unistd.h>
      #include <QThread>
      
      #include "QDebug"
      mongoUpdate::mongoUpdate(QObject *parent) :
      
          QObject(parent)
      {
          //     MONGO STARTS
              static const std::string mongo_uri = "mongodb://faces:faces@1xx.15x.43.84:27017/obj";
          //    const auto uri = mongocxx::uri::k_default_uri;
              const auto uri = mongo_uri;
      
              mongocxx::options::client client_options;
              mongocxx::instance inst{};
              mongocxx::client conn{mongocxx::uri{uri}};
          //    mongocxx::client conn{uri};
      
              auto db = conn["obj"];
              mongocxx::collection coll = db["objects"];
      
              qDebug() << "PARENT   Mongo Connected" << endl;
      
      
      }
      void mongoUpdate::send_mongo(QImage m_qimg)
      {
      
      write mongo some data when called from the main worker
      But here the mongo connection info  above is unknown ? . how can I  utilize above connection information here ?
      
      
      }
      
      
      
      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @RahibeMeryem last edited by

        @RahibeMeryem said in updating db through QThread (movetothread):

        But here the mongo connection info above is unknown

        Do you mean your conn variable you declared in the constructor?
        Yes, it isn't available here because it is local.
        Why don't you declare it as class variable?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • R
          RahibeMeryem last edited by

          Eh, due to be an java girl, c++ sometimes look alien.

          Bythe way I found a way to make the code working.

          Now the problem is mongodb update thread blocking all GUI even it is running a different thread.

          V 1 Reply Last reply Reply Quote 0
          • V
            VRonin @RahibeMeryem last edited by

            @RahibeMeryem said in updating db through QThread (movetothread):

            due to be an java girl

            It shows, you are leaking every single object you create. Welcome to the hell where no garbage collector can save you

            mongo->moveToThread(thread_mongo); what is thread_mongo?

            "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

            R 1 Reply Last reply Reply Quote 1
            • mrdebug
              mrdebug last edited by

              Hi. If you need to share your main db connection between threads I suggest this approach:

                              QString SessionName= SessionGet(); 
                              QSqlDatabase ThreadDb= QSqlDatabase::cloneDatabase(MainClassGlidePROServer->db, SessionName); {
                                  if (ThreadDb.open()) {
                                          QSqlQuery query(ThreadDb);
                                          //
                              }{
                                  ThreadDb= QSqlDatabase();
                                  ThreadDb.removeDatabase(SessionName);
                              }
              

              where
              SessionGet(); // something
              MainClassGlidePROServer->db // the connection defined in the main class.

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              V 1 Reply Last reply Reply Quote 0
              • R
                RahibeMeryem @VRonin last edited by

                @VRonin

                private:
                    Ui::Widget *ui;
                
                    QThread *thread;
                    QThread *thread_mongo;
                
                
                    Worker *worker;
                
                    mongoUpdate *mongo;
                

                you right we were comfortable and coosy with java :)

                1 Reply Last reply Reply Quote 0
                • V
                  VRonin @mrdebug last edited by

                  @mrdebug said in updating db through QThread (movetothread):

                  MainClassGlidePROServer->db

                  • I think that's still a race condition
                  • You should not keep a QSqlDatabase as a class member. http://doc.qt.io/qt-5/qsqldatabase.html :

                  Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

                  @RahibeMeryem said in updating db through QThread (movetothread):

                  QThread *thread_mongo;

                  You never assign anything to it so mongo->moveToThread(thread_mongo); should either do nothing or crash your application

                  "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 Reply Quote 2
                  • First post
                    Last post