PDA

View Full Version : How to Implement Sleep() or wait() in Qt app



sosanjay
29th December 2009, 09:35
Hi All,

I want to Implement Sleep() or wait() in Qt.

I have used QTest::sleep(250); but it not work properly in my application.

Is there any another option that I can use?

squidge
29th December 2009, 09:47
How about ::Sleep(ms) ?

But what do you mean by "not work properly" ?

sosanjay
29th December 2009, 09:57
Actually I am using Thread and I want to Pause the main application for few sec to receive the data but when I Implement QTest class I got error, It might be possible that I am not implementing properly. Please help me how to use the Sleep() in Qt.

high_flyer
29th December 2009, 10:06
Actually I am using Thread and I want to Pause the main application for few sec to receive the data
I don't know what you design is, but this sounds like trouble.
You should not make the main GUI thread wait, it also makes no sense, if you are using threads, that is what thread are for - to allow parallel execution.
If you want the application thread to wait, you actually don't need another thread, just do your task in the main thread, it will then wait for your task to end.
But note, that as long as the GUI thread waits for your task to finish, the application will be non responsive, since no events will be handled by the application.

Never the less, QThread has a public wait and protected sleep functions.

squidge
29th December 2009, 11:30
Actually I am using Thread and I want to Pause the main application for few sec to receive the dataIs there a reason why the thread can not send signal to main application thread to state when the data is ready instead of waiting for it? As high_flyer says, waiting in my thread is very bad. On Windows, this can cause the OS to place the text "Not Responding" on your window, as you will not be replying to messages from the OS.

vcernobai
29th December 2009, 20:02
Read more about QWaitCondition. There is a wait() function that is what you need.