The correct method for Qt program to wait for sleep

Source: Internet
Author: User

BKJIA Editor's note: the Qt framework is the main framework for Symbian application development and MeeGo application development. We often encounter this requirement when writing Qt programs: Let the program wait for a while, during this time, the program may not do anything, or it may have opened a sub-thread in the background to do some invisible work.

The first thing we can easily think of is to use the "sleep" or "usleep" function provided by linux. The Qter who has used this set of functions knows that this set of functions will change your current thread/process to "Sleep" state. This "Sleep" refers to deep sleep. during sleep, the kernel will not be assigned to the program time slice, so the program will not do anything, so you do not need to mention the interface refresh. The direct cause is that the user cannot interact with the program. Therefore, using sleep function to sleep directly is one of the common error solutions.

Another common error method is to use QTimer + endless loop. Similar to the following code:

 
  1. QTimer t; 
  2. t.start(); 
  3. while(t.elapsed() < 250); 

This simple and crude solution does not work. From the code, we can find that the while LOOP continuously calls the elapsed () function, which means that the CPU has no chance to do anything else during this period of time. Especially in non-preemptible operating systems such as Linux, the impact of such an endless loop is fatal, the CPU is fully occupied, and the kernel has no chance to schedule the process, other programs cannot execute time slices, and the system is basically paralyzed. In any case, this result is not what we want. Of course, unless you want to write a virus program .) For our program itself, although it occupies all the CPU, but because it falls into this loop, the program has no chance to enter the GUI event loop, resulting in the same interface cannot be refreshed.

In fact, the above Code is slightly changed to get a good solution. Step 1: solve the problem that the interface cannot be refreshed. Call QCoreApplication: processEvents (). The Code is as follows:

 
  1. QTimer t; 
  2. t.start(); 
  3. while(t.elapsed() < 250) 
  4. { 
  5. QCoreApplication::processEvents(); 
  6. } 

Step 2: solve the problem of high CPU usage-make the program sleep properly.

 
  1. QTimer t;
  2. T. start ();
  3. While (t. elapsed () <250)
  4. {
  5. QCoreApplication: processEvents ();
  6. Usleep (10000); // both sleep and usleep have been obsolete. We recommend that you use nanosleep instead.
  7. }

Setting the program to sleep for a short period of time won't feel much for users, but it makes a big difference to the kernel. In this way, the kernel has enough time to schedule the process/thread, so that other programs have the opportunity to execute.

In Qt, similar APIs are provided, but they are not in the core or gui library, but in QTestLib. Refer to the following documents:Http://doc.qt.nokia.com/4.6/qtest.html#qWait

This function is designed for Qt testing, so it is not recommended to use it directly in the Qt program. However, its basic implementation is almost the Code listed above. It's easy to copy paste to the program.

Phone Club -- BKJIA mobile development offline technology salon activity Date: January 15
Topic: advanced Android Application Development Technology
Location: donglian art club, No. 30, Zhongguancun South Street, Haidian District, Beijing
Lecturer: Fan huaiyu Netease) Click to register

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.