tags:

views:

82

answers:

1

hello,

In my application I want that when a loop is being executed,each time the control transfers to the loop,each execution must be delayed with a particular time.Can anyone tell me how can i do this?

Thank you.

+3  A: 

EDIT (removed wrong solution). EDIT (to add this other option):

Another way to use it would be subclass QThread since it has protected *sleep methods.

QThread::usleep(unsigned long microseconds);
QThread::msleep(unsigned long milliseconds);
QThread::sleep(unsigned long second);

Here's the code to create your own *sleep method.

#include <QThread>    

class Sleeper : public QThread
{
public:
    static void usleep(unsigned long usecs){QThread::usleep(usecs);}
    static void msleep(unsigned long msecs){QThread::msleep(msecs);}
    static void sleep(unsigned long secs){QThread::sleep(secs);}
}

and you call it by doing this:

Sleeper::usleep(10);
Sleeper::msleep(10);
Sleeper::sleep(1);

This would give you a delay of 10 microseconds, 10 milliseconds or 10 seconds, accordingly.

Hope this helps.

Live
This will pause the calling thread for a maximum time indicated by the milliseconds parameter or until the QThread completes its task. In other words, this pauses a thread until another thread completes. I don't think this is what the OP is looking for.
Arnold Spence
I'm pretty sure that if you have only a thread and specify a number of milliseconds lower than ULONG_MAX this thread will wait for the number of specified milliseconds.
Live
Ok, I think your alternate solution will work (haven't tried it) but the first option won't work if there is only one thread. If you call QThread::currentThread->wait(), Qt will report an error that a thread tried to wait on itself.
Arnold Spence
Edited my post, after Arnold Spence pointed out the first solution was not working. Thx Arnold.
Live
+1 A quick test of your second solution works well :)
Arnold Spence
this is not working as the code is giving an error as forward declaration of struct Qthread.
neha
You have to #include <QThread>, I edited my post to correct this.
Live
error appearing in the code:" error: incomplete type ‘QThread’ used in nested name specifier in the line static void usleep(unsigned long usecs){QThread::usleep(usecs);}
neha
thanks,its worked.
neha
but when i am using it within the painter event to draw 5 rect each after delay of 500ms,it hangs up for a times,and thn paints all the rects at same time.no delay.any suggestion??
neha
You have to sleep between every time you draw a rectangle. It should work even in a paint event.
Live
my code is: for(int i=0;i<5;i++) { std::stringstream ss; ss << arr[i]; ch=ss.str(); QString qstr = QString::fromStdString(ch); painter.drawRect(x, y, 60, 60); painter.drawText(x+25,y+25,qstr); x=x+60;Sleeper::msleep(500);}
neha
but this is not working properly as all the rectangles are drawn at same time and the application pauses before drawing them.
neha
Do you have more than one Thread?
Live
no,i am using this one only.
neha
Does the application hangs for 500ms or 2.5 seconds?
Live
and the speed of each operation in the application got very slow after applying the thread.it responds very late to the signals.why?
neha
It hangs up for 2.5 secs.
neha
It is said in Qt doc that there is optimization in the paintEvent() method. Maybe it is a thread by itself and cannot be but to sleep or adds up all the time it must sleep and then sleep for a longer time. If you try the Sleep in another method or simply to print a debug message does it work?
Live
how to do this with paintevent?
neha
The whole events system is handled by your QApplication and it processes events regularly. Since your paintEvent is treated as an event, it is processed by your QApplication.
Live
You could try this but I have never tested it. qApp->thread()->msleep(500); This is not perfect because your main thread will be completey frozen for half a second. If it works for you it is good, but be careful with this.
Live