PDA

View Full Version : Problem with sleep() and show()



lucasbemo
20th December 2010, 13:52
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.




QWidget *wid = this;
wid->setVisible(true);
wid->show();

#ifdef Q_OS_MAC
sleep(300);
#endif
#ifdef Q_OS_WIN
Sleep(3000);
#endif


Thanks in advance.

Lykurg
20th December 2010, 14:16
It is all about on how to keep the ui resposive. See this article here: http://doc.trolltech.com/qq/qq27-responsive-guis.html

ChrisW67
20th December 2010, 23:37
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:


qApp->processEvents();

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.

xiaokui
21st December 2010, 01:38
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.

lucasbemo
21st December 2010, 14:57
Thank you all. :D
I can make work properly with tips from you.