where's the sleep() func?
another easy question:
I need a simple delay/sleep in a loop - how do I do it? without getting into threading.
Code:
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
Re: where's the sleep() func?
is this for testing purposes?
If yes, have a look at QTest::qSleep ( int ms )
Cheers
Re: where's the sleep() func?
:( I can find neither a "QTest" nor a "qSleep" in my docs!
Is it part of the commercial lisence extras ?
thanks
K
Re: where's the sleep() func?
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.
Re: where's the sleep() func?
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
Re: where's the sleep() func?
Re: where's the sleep() func?
Quote:
Originally Posted by high_flyer
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
Re: where's the sleep() func?
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
Re: where's the sleep() func?
Code:
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?
Re: where's the sleep() func?
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
Re: where's the sleep() func?
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-intere...ad00380-0.html
from that thread:
Code:
#include <qthread.h>
{
public:
static void sleep(unsigned long secs) {
}
static void msleep(unsigned long msecs) {
}
static void usleep(unsigned long usecs) {
}
};
Re: where's the sleep() func?
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