Results 1 to 4 of 4

Thread: processEvents() for a QThread

  1. #1
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question processEvents() for a QThread

    I am using a QThread to do some large-scale computation in a worker thread. If there is nothing to do, however, the worker thread is waiting in a wait condition, similar to how it is done in the Mandelbrot example. My thread class works well and has non-trivial logic (when to enter an event loop, exit it again, go to sleep in the wait condition etc), , so I would like to keep it that way.

    Now I realized that I need cross-threads (queued) signal/slot connections for objects that live in the worker thread. Of course, the slots are not executed if the thread is currently in a wait condition.

    My idea was now to add a function to the thread class that would execute all pending events, and then go to sleep again in the wait condition. That way, I could emit my signals, and then make sure to wake up the thread to execute the corresponding slots.

    Unfortunately, although QThread have an event loop, there is no such thing as QThread:rocessEvents(). Is there any workaround to achieve what I want?

  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: processEvents() for a QThread

    All you need is to wake the thread and create a single shot timer with a 0 timeout. The timeout slot will be called after all pending events are processed (due the nature of timer implementation) and you can put the thread back to sleep again there.

  3. The following 2 users say thank you to wysota for this useful post:

    AlphaWolf (20th April 2009), hb (25th September 2007)

  4. #3
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: processEvents() for a QThread

    Thank you once again, wysota. Your proposed solution works flawlessly.

  5. #4
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: processEvents() for a QThread

    Seems like it is not working this way anymore. Any Ideas?

    Edit:
    Ah, it is because I am trying to interrupt a slot doing heavy work. Seems like I have to use processEvents().
    Last edited by DesertF0x; 17th March 2011 at 15:59.

Similar Threads

  1. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51
  2. Qthread Issue?
    By vishal.chauhan in forum Newbie
    Replies: 3
    Last Post: 29th March 2007, 08:50
  3. QGraphicsScene and QThread
    By tts80 in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2007, 09:32
  4. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22: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.