For a few days I'm still struggling to make this code work. I have one database (SQLITE), to which I connect at the beginning of the program.
Qt Code:
  1. QString path: "xxxxx";
  2. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  3. db.setDatabaseName(path);
  4. if(db.open())
  5. qDebug()<<"database"<<path<<"opened";
  6. else {
  7. qDebug()<<"unable to open database"<<path;
  8. }
  9.  
  10. /* FOR THE SAKE OF EXAMPLE
  11.   QSqlQuery query;
  12.   bool result=query.exec("CREATE TABLE IF NOT EXISTS compdefs "
  13.   "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
  14.   "name VARCHAR(20), "
  15.   "minarea INTEGER, "
  16.   "aspectratio DOUBLE,"
  17.   "orientationmode INTEGER,"
  18.   "upsample INTEGER,"
  19.   "horizontalflip BOOLEAN NOT NULL CHECK (horizontalflip IN (0,1)),"
  20.   "verticalflip BOOLEAN NOT NULL CHECK (verticalflip IN (0,1)),"
  21.   "c DOUBLE,"
  22.   "eps DOUBLE,"
  23.   "numthreads INTEGER,"
  24.   "trainedfhog BOOLEAN NOT NULL DEFAULT 0,");
  25.  
  26. */
To copy to clipboard, switch view to plain text mode 

Everything work fine, I can add, or remove from database as I want. But, in one moment, I need to push a button, this button create a QtConcurentRun, which call another thread to do my training HOG classifier function, and my main thread keep my GUI unfreeze!

But when my train HOG classifier function is complete, i need to save those data in my sqlite database, which is in my main thread and when I try to save those data, I get this error:
Qt Code:
  1. QSqlDatabasePrivate::database: requested database does not belong to the calling thread.
To copy to clipboard, switch view to plain text mode 
Is any method to update my database from the second thread? Maybe create a second connection? How I do this? If I create a second connection I need to remove fisrt connection? Please, I need some help.

This is my part of code, when I push the button.

Qt Code:
  1. void CompDefDialog::on_fhogTrainBtn_clicked(){
  2. //other stuff
  3. trainHOGFuture = QtConcurrent::run(compDef, &CompDef::trainFHOG);
  4. }
To copy to clipboard, switch view to plain text mode 


and this is my trahinFHOG function who start in the second thread:


Qt Code:
  1. void CompDef::trainFHOG(void)
  2. {
  3. QElapsedTimer timer;
  4. timer.start();
  5. //other stuff
  6. // from this line, I start to save my new data in my database
  7. if(id<=0)
  8. {
  9. qDebug()<<"save component definition first";
  10. return;
  11. }
  12.  
  13. trainedFHOG=true;
  14. oldFHOG=false;
  15.  
  16. QSqlQuery query;
  17. std::ostringstream buf;
  18. dlib::serialize(fhogDetector, buf);
  19.  
  20. QString sql="UPDATE compdefs SET fhogclassifier= :classifier, "
  21. "trainedfhog= :trainedfhog, "
  22. "oldfhog= :oldfhog "
  23. "WHERE id=:id";
  24.  
  25. query.prepare(sql);
  26. query.bindValue(":classifier", QByteArray(buf.str().c_str(),buf.str().length()));
  27. query.bindValue(":id", id);
  28. query.bindValue(":trainedfhog", trainedFHOG);
  29. query.bindValue(":oldfhog", oldFHOG);
  30.  
  31. if(!query.exec())
  32. {
  33. qDebug()<<"error while saving fhog classifier:"<<query.lastError()<<query.lastQuery();
  34. qDebug() << QSqlDatabase::drivers();
  35. }
  36. }
To copy to clipboard, switch view to plain text mode