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. Update Data to mongodb through Qthread..
QtWS25 Last Chance

Update Data to mongodb through Qthread..

Scheduled Pinned Locked Moved Solved General and Desktop
qthread threadsqthreadqt5.11qtcreator 5.0multithreading
6 Posts 3 Posters 871 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.
  • V Offline
    V Offline
    Vivek_A
    wrote on 22 Jan 2022, 09:38 last edited by
    #1

    Iam trying to send data to mongodb (Mongocxx) through qthread because its freeze main GUI while data inserting to db .
    I implemented a sample below , but still have the gui freeze . i think something missing in threading .. please look at the code and help please .

    in RTLSClient.cpp

    onethread = new myThread(this);
    connect(this,&RTLSClient::sendtodb,onethread,&myThread::recvNum);
    onethread->start();
    }
    void RTLSClient::newData()
    {
       while(this->socket->bytesAvailable())
         {
           qint64 dataLen = this->socket->peek(data, sizeof(data));
           emit sendtodb(2.0,3.0);
         }
    }
    myThread::myThread(QObject *parent)
       : QThread(parent)
    {
    }
    
    void myThread::recvNum(double x,double y)
    {
    //   this->m1=x;
    //   this->m2=y;
       std::vector<bsoncxx::document::value> documents;
       documents.push_back(
       bsoncxx::builder::stream::document{} <<"bool"<<1 <<"POSX"<<x<<"POSY"<<y<<finalize);
       db["xxx"].insert_many(documents);
       qDebug()<<"threadLast";
    }
    
       ``
    DATA getting db but GUI freeze when more data coming ....

    vk

    J C 2 Replies Last reply 22 Jan 2022, 09:42
    0
    • C Christian Ehrlicher
      22 Jan 2022, 12:49

      @Vivek_A said in Update Data to mongodb through Qthread..:

      connect(this,&RTLSClient::sendtodb,onethread,&myThread::recvNum);

      Please take a look into the QThread documentation on why this slot is executed in the main thread. QThread is not a thread, it's a class which controls a thread!

      V Offline
      V Offline
      Vivek_A
      wrote on 24 Jan 2022, 05:09 last edited by
      #6

      @Christian-Ehrlicher @JonB Solved . By creating a seperate worker.cpp file and connected RTLSClient signal to worker slot, now data is inserting to mongodb very fast without freezing GUI.
      Thanks ..

      vk

      1 Reply Last reply
      0
      • V Vivek_A
        22 Jan 2022, 09:38

        Iam trying to send data to mongodb (Mongocxx) through qthread because its freeze main GUI while data inserting to db .
        I implemented a sample below , but still have the gui freeze . i think something missing in threading .. please look at the code and help please .

        in RTLSClient.cpp

        onethread = new myThread(this);
        connect(this,&RTLSClient::sendtodb,onethread,&myThread::recvNum);
        onethread->start();
        }
        void RTLSClient::newData()
        {
           while(this->socket->bytesAvailable())
             {
               qint64 dataLen = this->socket->peek(data, sizeof(data));
               emit sendtodb(2.0,3.0);
             }
        }
        myThread::myThread(QObject *parent)
           : QThread(parent)
        {
        }
        
        void myThread::recvNum(double x,double y)
        {
        //   this->m1=x;
        //   this->m2=y;
           std::vector<bsoncxx::document::value> documents;
           documents.push_back(
           bsoncxx::builder::stream::document{} <<"bool"<<1 <<"POSX"<<x<<"POSY"<<y<<finalize);
           db["xxx"].insert_many(documents);
           qDebug()<<"threadLast";
        }
        
           ``
        DATA getting db but GUI freeze when more data coming ....
        J Offline
        J Offline
        JonB
        wrote on 22 Jan 2022, 09:42 last edited by JonB
        #2

        @Vivek_A
        Put qDebug() statements at the beginning, end and in the middle of RTLSClient::newData(). I don't know how you think that is going to function.

        Not to mention, you don't actually show any call to it, your code doesn't show much of what must actually be going on elsewhere in it.

        V 1 Reply Last reply 22 Jan 2022, 09:58
        0
        • J JonB
          22 Jan 2022, 09:42

          @Vivek_A
          Put qDebug() statements at the beginning, end and in the middle of RTLSClient::newData(). I don't know how you think that is going to function.

          Not to mention, you don't actually show any call to it, your code doesn't show much of what must actually be going on elsewhere in it.

          V Offline
          V Offline
          Vivek_A
          wrote on 22 Jan 2022, 09:58 last edited by
          #3

          @JonB The source code very lengthy ... one thing i can say .. in that RTLSClient.cpp there is lot of emit signals given for GraphicsWidget gui update . and from socket takinng data and update to gui and trying to send to db .. this is the logic .
          And i posted the same in mongocxx forum , no reply yet ,
          same scenario like this updating db through QThread except the code ..

          vk

          J 1 Reply Last reply 22 Jan 2022, 11:20
          0
          • V Vivek_A
            22 Jan 2022, 09:58

            @JonB The source code very lengthy ... one thing i can say .. in that RTLSClient.cpp there is lot of emit signals given for GraphicsWidget gui update . and from socket takinng data and update to gui and trying to send to db .. this is the logic .
            And i posted the same in mongocxx forum , no reply yet ,
            same scenario like this updating db through QThread except the code ..

            J Offline
            J Offline
            JonB
            wrote on 22 Jan 2022, 11:20 last edited by
            #4

            @Vivek_A
            I suggested you put 3 qDebug() statements into your RTLSClient::newData(), before loop, in loop, after loop, to see what is going on. I do not see why you think that loop will ever exit. Up to you.

            1 Reply Last reply
            0
            • V Vivek_A
              22 Jan 2022, 09:38

              Iam trying to send data to mongodb (Mongocxx) through qthread because its freeze main GUI while data inserting to db .
              I implemented a sample below , but still have the gui freeze . i think something missing in threading .. please look at the code and help please .

              in RTLSClient.cpp

              onethread = new myThread(this);
              connect(this,&RTLSClient::sendtodb,onethread,&myThread::recvNum);
              onethread->start();
              }
              void RTLSClient::newData()
              {
                 while(this->socket->bytesAvailable())
                   {
                     qint64 dataLen = this->socket->peek(data, sizeof(data));
                     emit sendtodb(2.0,3.0);
                   }
              }
              myThread::myThread(QObject *parent)
                 : QThread(parent)
              {
              }
              
              void myThread::recvNum(double x,double y)
              {
              //   this->m1=x;
              //   this->m2=y;
                 std::vector<bsoncxx::document::value> documents;
                 documents.push_back(
                 bsoncxx::builder::stream::document{} <<"bool"<<1 <<"POSX"<<x<<"POSY"<<y<<finalize);
                 db["xxx"].insert_many(documents);
                 qDebug()<<"threadLast";
              }
              
                 ``
              DATA getting db but GUI freeze when more data coming ....
              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 22 Jan 2022, 12:49 last edited by
              #5

              @Vivek_A said in Update Data to mongodb through Qthread..:

              connect(this,&RTLSClient::sendtodb,onethread,&myThread::recvNum);

              Please take a look into the QThread documentation on why this slot is executed in the main thread. QThread is not a thread, it's a class which controls a thread!

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              V 1 Reply Last reply 24 Jan 2022, 05:09
              2
              • C Christian Ehrlicher
                22 Jan 2022, 12:49

                @Vivek_A said in Update Data to mongodb through Qthread..:

                connect(this,&RTLSClient::sendtodb,onethread,&myThread::recvNum);

                Please take a look into the QThread documentation on why this slot is executed in the main thread. QThread is not a thread, it's a class which controls a thread!

                V Offline
                V Offline
                Vivek_A
                wrote on 24 Jan 2022, 05:09 last edited by
                #6

                @Christian-Ehrlicher @JonB Solved . By creating a seperate worker.cpp file and connected RTLSClient signal to worker slot, now data is inserting to mongodb very fast without freezing GUI.
                Thanks ..

                vk

                1 Reply Last reply
                0

                6/6

                24 Jan 2022, 05:09

                • Login

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