Results 1 to 5 of 5

Thread: Problem with sleep() and show()

  1. #1
    Join Date
    Mar 2010
    Location
    Brasília, Brasil
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Problem with sleep() and show()

    I wonder, why in the following code is executed first to sleep() function and then executes the show() function. If anyone has an idea for it to execurtar the first show () to after the sleep () I would appreciate it.


    Qt Code:
    1. QWidget *wid = this;
    2. wid->setVisible(true);
    3. wid->show();
    4.  
    5. #ifdef Q_OS_MAC
    6. sleep(300);
    7. #endif
    8. #ifdef Q_OS_WIN
    9. Sleep(3000);
    10. #endif
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with sleep() and show()

    It is all about on how to keep the ui resposive. See this article here: http://doc.trolltech.com/qq/qq27-responsive-guis.html

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with sleep() and show()

    The show() method executes before the sleep() call. However, Qt will not act on the request to show the UI before it returns to the event loop, which is some time after the sleep() period. You can put:
    Qt Code:
    1. qApp->processEvents();
    To copy to clipboard, switch view to plain text mode 
    after the call to show() to force immediate processing.

    There is probably a better way to achieve what you want. What is the purpose of the delay? What do you want to do after the sleep period is done?

    Other notes:
    Line 2 and line 3 do the same thing.
    You can use QTimer for platform independent timeouts.

  4. #4
    Join Date
    Dec 2010
    Location
    BEIJING CHINA
    Posts
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with sleep() and show()

    en, This is very interesting. But there is no magic.
    As i know, there is two simple reason for this.
    firstly, every GUI library has one and only one GUI main thread, your "sleep()" will block this GUI thread.
    secondly, every GUI library has a event pump. every event will be published by event pump. your code "show()" do not run immediately like C code.
    the real fact in your simple code is very complex than I say.
    just a tip.

  5. #5
    Join Date
    Mar 2010
    Location
    Brasília, Brasil
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Problem with sleep() and show()

    Thank you all.
    I can make work properly with tips from you.

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. Replies: 1
    Last Post: 2nd May 2010, 08:53
  3. Sleep condition in QT
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2008, 12:07
  4. sleep
    By sonuani in forum Newbie
    Replies: 1
    Last Post: 17th March 2008, 12:37
  5. sleep - sleeps, but not when I want it to sleep
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 14: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.