Update Data to mongodb through Qthread..
-
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 ....
-
@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 .. -
@Vivek_A
PutqDebug()
statements at the beginning, end and in the middle ofRTLSClient::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.
-
@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 .. -
@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!
-
@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 ..