Results 1 to 2 of 2

Thread: Multiple QEventLoops in the MainThread QtGui

  1. #1
    Join Date
    Feb 2013
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Multiple QEventLoops in the MainThread QtGui

    Hi all,

    I have some Problems (perhaps some missunderstood) with multiple QEventLoops in the MainThread. First of all the reason how I got to this problems
    Some pseudo code (wont compile)...
    Example1:
    Qt Code:
    1. void example::method1(){
    2. /** this pseudo code shows a simple use of qsqlquery and executes 2 select statements (2nd select use the result value from qry1)
    3. */
    4. QSqlQuery qry("Select col1, col2 from tbl where id = 5");
    5. qry.exec();
    6. qry.first();
    7. qry.exec("Select col3 from tbl2 where id = qry.value(0).toInt()....);
    8. qry.first();
    9. qDebug() << "value col3 tbl2 with id=5 from tbl1" << qry.value(0);
    10. }
    To copy to clipboard, switch view to plain text mode 
    So far so good, now i have written a simple sqlquery class with signal slot to execute queries not blocking the main gui thread...
    Example2:
    Qt Code:
    1. void example::method2(){
    2. // Pseudo code
    3. SqlQuery qry("Select...);
    4. connect thread started to qry execute, and the signal qry finished() with slot: example::method3(...)
    5. }
    6. void example::method3(...){
    7. //use the result from qry execute method2 and execute a new qry with the result...
    8. Sqlqry ("select.... );
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    As good as Signal/Slots are, the Problem in example2 is that it will become really hard to read if u normally use multiple qry.execute() in one method() and now with multiple slots/methods...

    So I thought about writing a "wrapper" for the SqlQuery class...
    The Problem is that I have to start a QEventLoop(in the main thread) to wait for the qry result and the other Problem is that if I trigger another qry while an EventLoop(created in the MainThread by the wrapper) hasn´t finished yet (waiting for the qry to be finished), another Eventloop will be created in the main thread and the first created Eventloop will be left after the 2nd created Eventloop has finished (First in Last Out...). So If I would start a qry which took 5 seconds and start another qry while the first still running and the second one would take 20 seconds, all the results would be available after 20 seconds (The first qry has finished after 5 seconds but the 2nd eventloop hasn´t quit yet so I have to wait 20 seconds till the results are there/available in the main thread...)

    Sorry for the long and perhaps confusing description ...

    I dont have any ideas how to solve my small Problem. (use threaded Sqlquery without rewriting/change the default useage...)
    I am really thankful for any ideas

    Wish u all a nice day.

    Ane

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple QEventLoops in the MainThread QtGui

    Your main thread usually runs an event loop. QApplication::exec() starts it.

    I am afraid I don't understand why you separate the sequence into different slots and not just have all queries within one slot.

    It also looks like what you are actually looking for it either implementing QThread::run() or QThreadPool+QRunnable subclasses

    Cheers,
    _

Similar Threads

  1. MainThread and QThread
    By Qiieha in forum Qt Programming
    Replies: 8
    Last Post: 22nd May 2012, 12:54
  2. Calling QThread.wait() from mainthread
    By P@u1 in forum Qt Programming
    Replies: 3
    Last Post: 1st June 2011, 15:41
  3. QtBrowserPlugin without QtGui.dll?
    By anupamgee in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2010, 14:52
  4. Replies: 0
    Last Post: 26th February 2010, 19:01
  5. using QtGui
    By nupul in forum Qt Programming
    Replies: 5
    Last Post: 7th April 2006, 12:36

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.