PDA

View Full Version : where's the sleep() func?



TheKedge
2nd February 2006, 14:53
another easy question:

I need a simple delay/sleep in a loop - how do I do it? without getting into threading.


for(int i=0; i<4; ++i)
{
//do something with data[i]
sleep(50);//for example
}
a very silly example I know ... but using this could save me making 4 timers.
and yes, generally, it must be a bad idea to put sleep()s into the main thread... but still, it could be handy.

thanks
K

Everall
2nd February 2006, 15:10
is this for testing purposes?
If yes, have a look at QTest::qSleep ( int ms )

Cheers

TheKedge
2nd February 2006, 15:13
:( I can find neither a "QTest" nor a "qSleep" in my docs!
Is it part of the commercial lisence extras ?
thanks
K

high_flyer
2nd February 2006, 15:19
QThread::sleep ( unsigned long secs )
QThread::usleep ( unsigned long usecs )
QThread::msleep ( unsigned long msecs )

But it depends what you would like to acheave.
Some things can be done nicer with a slot connected to a QTimer.

Everall
2nd February 2006, 15:22
I'm using a Qt/X11 Open Source 4.1.0 snapshot

What version are you using. If I remember well, this was new to 4.1.

Cheers

high_flyer
2nd February 2006, 15:27
no, Qt3X had it as well.

Everall
2nd February 2006, 15:31
no, Qt3X had it as well.

I think we are talking about different things here.
I was referring to
QTest::qSleep ( int ms )
which is new to QT4.1 i believe

not
QThread::sleep ( unsigned long secs )...
which was in Qt3X already as you said.

Our posts must have been made at about the same time.

Cheers

TheKedge
2nd February 2006, 15:51
Thanks, that's exactly what I need. Erm, just one thing .. :o if I call
QThread::msleep(10);
I get a compiler error cos it's static protected. I'm not going to have to subclass my object am I? i.e. inheirit QThread?

Kev

Everall
2nd February 2006, 16:20
I'm not going to have to subclass my object am I? i.e. inheirit QThread?

To create your own threads, you have to subclass QThread and reimplement run()

By the way, have you checked your QT version?

TheKedge
2nd February 2006, 16:30
Sure, but I don't want another thread. I just want a sleep() func - something to suspend the main thread. A QThread::sleep() i.e. a public static func, would be perfect. Or alternativly, a QThread::currentThread()->sleep() would also do nicely. But I can't do it that way cos the sleep funcs are static protected.

anyway, I'm running 4.0.1 I guess I can update and as you suggest try a QTest::

thanks
K

high_flyer
2nd February 2006, 16:39
Yes but sleep IS per thread.
And makes no sense to make a GUI thread (the main thread) sleep.
I don't know however what happens if you want to use sleep in a console application with Qt4...
But this thread might prove interesting for you:
http://lists.trolltech.com/qt-interest/2005-04/thread00380-0.html
from that thread:

#include <qthread.h>

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

TheKedge
2nd February 2006, 16:54
Well, you know what? that's exactly what I want to do, and that's exactly why I want to do it. And, I think, that's exactly how I'll do it!

thanks very much,
Kev