Results 1 to 4 of 4

Thread: QThread event loop seems blocked

  1. #1
    Join Date
    May 2009
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread event loop seems blocked

    Hi,
    I've read about threads and signals in some other topics but I couldn't really find something similar to this.

    I have a worker thread that is supposed to ping the GUI with information every now and then. The algorithm is similar to:

    Qt Code:
    1. while(hasWork())
    2. {
    3. emit Status()
    4. DoWork()
    5. emit Status()
    6. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that the slots connected to Status are only executed after all the work is done and the while loop has exited.

    Considering that:
    - I'm calling exec() in QThread::run()
    - I'm using queued connections
    - I'm starting the work by emitting a signal. The signal is connected to the slot of an object that lives in the thread and the slot is executed in the thread.
    - the slot and its parent object live in the GUI thread.

    ... I think that the thread's event loop is blocked while executing the slot that started the thread and can't process my emitted signals.

    Any ideas on getting around this issue?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread event loop seems blocked

    If you have a while loop then you don't give the event queue the chance to process events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread event loop seems blocked

    Yes, I was afraid of that... Is there a Qt pattern for situations like these when you want to issue updates to the GUI from a worker thread while in the middle of a big job?

    I was thinking of removing the event loop altogether and just calling
    Qt Code:
    1. postEvent(objectInGuiThread, MyEvent).
    To copy to clipboard, switch view to plain text mode 
    That way I *think* that the GUI event loop will take care of everything.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread event loop seems blocked

    You don't need an event loop in the thread you are emitting signals from - only in the one you are emitting them to.

    Anyway, have a look at this article: Keeping the GUI Responsive.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    eurodatar (6th May 2009)

Similar Threads

  1. Replies: 0
    Last Post: 23rd October 2008, 12:43
  2. Qt plug-in for GLib event loop based application
    By profoX in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2008, 14:27
  3. Glib event loop
    By Brandybuck in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2006, 17:19
  4. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  5. Replies: 4
    Last Post: 23rd January 2006, 16:51

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.