Results 1 to 15 of 15

Thread: Idle()

  1. #1
    qtoptus Guest

    Default Idle()

    How can I implement an idle event handler such that when the program is not processing user input events/GUI...? I tried the QTimer with interval 0 but it seems it calls the handler with non blocking manner that before returning from the event handler it may be called again...

  2. #2
    Join Date
    May 2011
    Posts
    239
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60
    Thanks
    4
    Thanked 35 Times in 35 Posts

    Default Re: Idle()

    I don't know, but the traditional way to protect a handler is to use a flag to stop enty when previous processing is still underway:

    Qt Code:
    1. void myProcessor()
    2. {
    3. static bool processing = false;
    4.  
    5. if (processing) {
    6. return;
    7. }
    8. // processing =0 false
    9. processing = true;
    10. // now, do something
    11. // ...done...
    12. processing = false;
    13. }
    To copy to clipboard, switch view to plain text mode 






    }

  3. #3
    qtoptus Guest

    Default Re: Idle()

    Thanks for your reply. I can give it a try but would not this lead to race conditions on the entry-flag so for instance just before it's set another call to the handler occurs...I'm assuming here the timer runs in a separate thread.
    But if there's a way that I can hack into the application's main event loop and do something similar to traditional Win32 API non-blocking message loop (PeekMessage) that would be handy.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Idle()

    If a timer slot does not return control, the next timer is not called, and all the event processing stops, it is in deed blocking the event loop execution. As you expect the timer is called only once and will not be called again unless you return the the control.

    If you see (in your program), timer slot being called multiple times, then you are doing something in your code which cause it to happen. By default, if you don't return from timer slot, then it just stops processing all the events in the thread, where the timer lives.

  5. #5
    qtoptus Guest

    Default Re: Idle()

    All the the timer callback does is refreshing the rendering canvas by calling the widget's update/repaint. I use Direct3D for rendering, so my guess is that an asynchronous calls of the timer's callback causes a deadlock like situation where some Direct3D commands are called again before other calls finish, or maybe the same commands called twice before they finish.
    For example, a Direct3D device BeginScene is called again inside another BeginScene...

    That's why I'm looking for some reliable way to handle idle state without resorting to a
    "hack" solution which is the timer callback.

    There must be a way to add and handle idle events like in other GUI toolkits...

    Any idlea?
    Last edited by qtoptus; 2nd September 2011 at 20:37.

  6. #6
    qtoptus Guest

    Default Re: Idle()

    Ok so there's no idle event callback can be assigned to a Qt application, other than the timer hack...fair enough!

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Idle()

    Well I will say you don't need a idle event handler generally in a event based framework like Qt. If you need to update a GUI regularly, you can as well do in a timer event (callback) and call update on the widget (don't call paint directly). When you call update it will queue required events to repaint the widget (if required) and the widget will repaint only after control returns to the event loop, back from timer event. You need to figure out how often you need to refresh the GUI, set that as timeout. Again if you know what causes the GUI to be re-painted / updated, you could call the update on the widget from that event directly rather than relying on a timer event.

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

    Default Re: Idle()

    First of all it is not a hack and second of all you are obviously doing something wrong, the timer can't possibly trigger again before the previous iteration ends. I think you are getting some effect that incorrectly suggests that this is the case. For instance if you call update() from the timer's timeout slot, then it is true that timeout() might be called again before paintEvent() is executed (although it's unlikely). This is due to the fact that update() only schedules an event and doesn't do any repainting. However I agree with Santosh that you shouldn't need such calls at all. You need to update the canvas if it changes, there is no point in redrawing it with the same contents as before, it's just a waste of CPU cycles.
    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.


  9. #9
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    129
    Thanked 3 Times in 3 Posts

    Default Re: Idle()

    Quote Originally Posted by wysota View Post
    First of all it is not a hack and second of all you are obviously doing something wrong, the timer can't possibly trigger again before the previous iteration ends. I think you are getting some effect that incorrectly suggests that this is the case. For instance if you call update() from the timer's timeout slot, then it is true that timeout() might be called again before paintEvent() is executed (although it's unlikely). This is due to the fact that update() only schedules an event and doesn't do any repainting. However I agree with Santosh that you shouldn't need such calls at all. You need to update the canvas if it changes, there is no point in redrawing it with the same contents as before, it's just a waste of CPU cycles.
    Guessing the error is quite wow
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  10. #10
    qtoptus Guest

    Default Re: Idle()

    However I agree with Santosh that you shouldn't need such calls at all
    I disagree.

    You need to update the canvas if it changes, there is no point in redrawing it with the same contents as before, it's just a waste of CPU cycles.
    Well there's a big point when you are working on a robust application such as CAD. Otherwise this suggests that video games waste CPU cycles all the time
    Nope it's mainly GPU cycles, and they are not wasted since they are not in use when the scene is not changing.

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

    Default Re: Idle()

    Quote Originally Posted by qtoptus View Post
    Otherwise this suggests that video games waste CPU cycles all the time
    If they resend the same frame multiple times then they do. If you have a 60Hz display, you can render even 1000000FPS but you will only see 60. The rest is wasted.
    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.


  12. #12
    qtoptus Guest

    Default Re: Idle()

    If they resend the same frame multiple times then they do
    How would you resend the same frame multiple times and why? Put it in a loop inside the "timer" or the idle event?

    Well I can set the timer to 60 FPS Done!

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

    Default Re: Idle()

    Quote Originally Posted by qtoptus View Post
    How would you resend the same frame multiple times and why?
    Don't ask me. It's you who said that you don't agree that a new frame should be generated only when something on the display changes.
    Well I can set the timer to 60 FPS
    Sure you can. But this won't give you 60 FPS.
    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.


  14. #14
    qtoptus Guest

    Default Re: Idle()

    It's you who said that you don't agree that a new frame should be generated only when something on the display changes.
    Frame generation vs. rendering.

    Sure you can. But this won't give you 60 FPS.
    Then what 70, 80, 10 FPS?

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

    Default Re: Idle()

    Quote Originally Posted by qtoptus View Post
    Frame generation vs. rendering.
    So you want to generate 10 frames but display 10000 frames? In my opinion it is much better to generate 10 frames and display 10 frames (or less if the hardware can't keep up).

    Then what 70, 80, 10 FPS?
    Setting the timer to 60 fps will give you less than 60 frames per second top.
    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.


Similar Threads

  1. QThread and QEventLoop - Idle Processing
    By kloffy in forum Newbie
    Replies: 14
    Last Post: 28th April 2011, 11:41
  2. Detect application idle
    By chris_helloworld in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2011, 16:49
  3. Cross platform CPU idle timeout
    By skyphyr in forum Qt Programming
    Replies: 6
    Last Post: 9th December 2010, 17:53
  4. how do I get the idle time in qt2
    By pencilren in forum Qt Programming
    Replies: 1
    Last Post: 17th September 2007, 04:19
  5. detecting Idle Activity ?
    By nupul in forum Newbie
    Replies: 9
    Last Post: 5th April 2006, 07: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
  •  
Qt is a trademark of The Qt Company.