Quote Originally Posted by jpujolf View Post
I made this specialized thread class :
Qt Code:
  1. #include <QThread>
  2. #include <QSqlQuery>
  3.  
  4. class MyQueryThread : public QThread
  5. {
  6. QSqlQuery & m_Query;
  7. QString m_SQL;
  8.  
  9. public :
  10.  
  11. MyQueryThread ( QSqlQuery & Query, const QString & SQL ) :
  12. m_Query ( Query ), m_SQL ( SQL ) {}
  13.  
  14. void run ()
  15. {
  16. m_Query.exec ( m_SQL );
  17. }
  18. };
  19.  
  20. void ExecQueryWrapper ( QSqlQuery & Query, const QString & SQL )
  21. {
  22. MyQueryThread MQT ( Query, SQL );
  23. MQT.start();
  24. while ( MQT.isRunning() )
  25. qApp->processEvents ();
  26. };
To copy to clipboard, switch view to plain text mode 

You only have to wrap your call to exec through this function and all calls are made without freezing GUI.
I'm afraid it is not a good example code as QSqlDatabase connection does not support multithreading. So when you want to use database in a thread you have to create new database connection in every thread. And remember that in QThread class only the body of run() method is in separate thread, body of the QThread constructor for example (so constructing member variables) is in the constructors caller thread. So for eaxample creating connection to database (so queries using that connection also) sould be done in run() method and they sould be run() method local variables or created on a heap in run() thread (then you can have member pointer variables).