Results 1 to 6 of 6

Thread: QSqlQuery and no response while exec()

  1. #1
    Join Date
    Mar 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlQuery and no response while exec()

    Hello my friends. I am writing an application using MySQL database. The problem is, the database server is far, far away and it takes much time to get some results. Unfortunately, QSqlQuery.exec() causes application to freeze (no response) until I get response from the server. I am looking for easy and efficient way to stop these freezes or to inform users about ongoing process. Any suggestions?
    Last edited by jacek_; 3rd November 2009 at 14:59. Reason: spelling error

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery and no response while exec()

    I'm afraid you should use thread, i.e. move all SQL-queryies in separate thread and establish communication between main thread and worker thread using signals/slots mechanism.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Mar 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery and no response while exec()

    Quote Originally Posted by spirit View Post
    I'm afraid you should use thread, i.e. move all SQL-queryies in separate thread and establish communication between main thread and worker thread using signals/slots mechanism.
    That would require rewritting the whole program. I am trying to avoid that .

  4. #4
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: QSqlQuery and no response while exec()

    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery and no response while exec()

    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).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery and no response while exec()

    I didn't share your point of view. I agree QSqldatabase is not multithread. But take a better look at my code, please, before blaming it.

    I disagree in the fact that this piece of code is not working, because is currently in a production software and I have no problems with it. Sometimes reality comes and shows you're wrong...

    If you take a closer look at the code, you can see I DON'T execute multiple threaded calls to the database. I suggested to wrap the call, that locks the main thread whith a "working" while that allows GUI to show updates, mouse / window movements, etc.

    I proposed a wrapped call to avoid re-writing entire program, but allowing the GUI stay active wile running slow queries. It works, as I said before. And is simple to replace a call like this :
    Qt Code:
    1. Qry.exec ( "SELECT * FROM FAR_TABLE" )
    To copy to clipboard, switch view to plain text mode 

    to one like this

    Qt Code:
    1. ExecQueryWrapper ( Qry, "SELECT * FROM FAR_TABLE" )
    To copy to clipboard, switch view to plain text mode 

    without great effort...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.