Results 1 to 3 of 3

Thread: sleep () in Qt

  1. #1
    Join Date
    Jul 2012
    Location
    UK
    Posts
    17
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default sleep () in Qt

    Question.png

    Dear Qt Community,

    Request your help. We show a random character (A, B, C, ...) in a scene. When the user presses the key then a black window appears and stays for 200 msec. Thereafter another character appears in the screen and the user can make selection by pressing a key again. This process keeps repeating.


    If I use the following code to sleep, then the GUI freezes. We can not see the black window.

    Qt Code:
    1. class Sleep : public QThread
    2. {
    3. public:
    4. static void sleep(unsigned long secs) {
    5. QThread::sleep(secs);
    6. }
    7. static void msleep(unsigned long msecs) {
    8. QThread::msleep(msecs);
    9. }
    10. static void usleep(unsigned long usecs) {
    11. QThread::usleep(usecs);
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    The following code does not freeze the GUI, but it keeps taking the events when the window is blank. But when the window is blank we do not want any keypress event to be processed.

    Qt Code:
    1. QTime dieTime = QTime::currentTime().addSecs(2);
    2. while( QTime::currentTime() < dieTime )
    3. QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: sleep () in Qt

    You don't need any sleeping. Use a timer or QSequentialAnimationGroup. Or even the state machine framework.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    saifulkhan (2nd February 2013)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: sleep () in Qt

    You could do this with simple timer (no need of threads / sleeps), here is a working example
    Qt Code:
    1. #include <QWidget>
    2. #include <QPainter>
    3. #include <QKeyEvent>
    4. #include <QPaintEvent>
    5. #include <QTextOption>
    6. #include <QApplication>
    7.  
    8. class Widget : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Widget()
    14. : text(QString("Press Key To Start :)"))
    15. , textOption(Qt::AlignCenter)
    16. , timer(0)
    17. {
    18. setFixedSize(200, 200);
    19. }
    20. virtual ~Widget() {}
    21.  
    22. protected:
    23. void paintEvent(QPaintEvent * event)
    24. {
    25. if(timer == 0)
    26. QPainter(this).drawText(event->rect(), text, textOption);
    27. else
    28. QPainter(this).fillRect(event->rect(), QBrush(QColor(Qt::darkGray)));
    29. }
    30.  
    31. void keyPressEvent(QKeyEvent * event)
    32. {
    33. if(timer == 0)
    34. {
    35. if((event->key() >= Qt::Key_A) and (event->key() <= Qt::Key_Z))
    36. text = QString('A' + event->key() - Qt::Key_A);
    37. else
    38. text.clear();
    39.  
    40. timer = startTimer(2000); // 2 Seconds
    41. update();
    42. }
    43. }
    44.  
    45. void timerEvent(QTimerEvent * event)
    46. {
    47. if(timer == event->timerId())
    48. {
    49. timer = 0;
    50. update();
    51. }
    52. killTimer(event->timerId());
    53. }
    54.  
    55. private:
    56. QString text;
    57. QTextOption textOption;
    58. int timer;
    59. };
    60.  
    61. int main(int argc, char *argv[])
    62. {
    63. QApplication app(argc, argv);
    64. Widget w;
    65. w.show();
    66. return app.exec();
    67. }
    68.  
    69. #include "Main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    saifulkhan (2nd February 2013)

Similar Threads

  1. Help about sleep on C++ ...
    By Bong.Da.City in forum Qt Programming
    Replies: 4
    Last Post: 24th August 2010, 19:29
  2. How do you sleep when using a QTimer
    By newqtuser in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2008, 00:49
  3. sleep
    By sonuani in forum Newbie
    Replies: 1
    Last Post: 17th March 2008, 12:37
  4. sleep - sleeps, but not when I want it to sleep
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 14:43
  5. where's the sleep() func?
    By TheKedge in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2006, 15:54

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.