Results 1 to 12 of 12

Thread: where's the sleep() func?

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. for(int i=0; i<4; ++i)
    2. {
    3. //do something with data[i]
    4. sleep(50);//for example
    5. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: where's the sleep() func?

    is this for testing purposes?
    If yes, have a look at QTest::qSleep ( int ms )

    Cheers

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: where's the sleep() func?

    no, Qt3X had it as well.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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
    Last edited by Everall; 2nd February 2006 at 15:33.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: where's the sleep() func?

    Thanks, that's exactly what I need. Erm, just one thing .. 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

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: where's the sleep() func?

    Qt Code:
    1. I'm not going to have to subclass my object am I? i.e. inheirit QThread?
    To copy to clipboard, switch view to plain text mode 

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

    By the way, have you checked your QT version?

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. #include <qthread.h>
    2.  
    3. class I : public QThread
    4. {
    5. public:
    6. static void sleep(unsigned long secs) {
    7. QThread::sleep(secs);
    8. }
    9. static void msleep(unsigned long msecs) {
    10. QThread::msleep(msecs);
    11. }
    12. static void usleep(unsigned long usecs) {
    13. QThread::usleep(usecs);
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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

Similar Threads

  1. How do you sleep when using a QTimer
    By newqtuser in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2008, 01:49
  2. how to sleep with qt function?
    By yleesun in forum Qt Programming
    Replies: 2
    Last Post: 5th August 2008, 13:08
  3. Sleep condition in QT
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2008, 13:07
  4. sleep
    By sonuani in forum Newbie
    Replies: 1
    Last Post: 17th March 2008, 13:37
  5. sleep - sleeps, but not when I want it to sleep
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 15:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.