How to ensure smooth GUI response (QT platform)

Source: Internet
Author: User
 
Introduction

Generally, operations that take a long time are divided into computing-intensive operations and IO-intensive operations. How can these two types of operations increase the response speed?

In essence, operations can be divided into non-decomposed operations, such as operations that take a long time in a third-party library and operations that can be decomposed, among them, decomposed operations can be subdivided into serial operations and parallel operations. How can we improve the response speed for these operations?

How can I convert asynchronous operations, such as Asynchronous Data Reading methods in the network library, into synchronous operations?

In addition, the general idea of Multithreading is to provide program running speed. Otherwise, improper use of threads often slows down the program, in QT, can I increase the response speed through methods other than multithreading?

These questions will be answered one by one below.

 

Body

First, what is the response to the GUI? A: The response speed of the GUI is the processing speed of the system for GUI events.

Because it takes some time for the system to process events, the window system generally provides an event queue to store events. If you treat each event as a task, event processing is similar to the operating system processing tasks by priority, minimizing the average wait time of each task. You can refer to the methods in the operating system, for example:

Grading to delay execution of long tasks.

For a long task, wait for a period of time before running the task.

Reduce the time consumed by each task. Of course, this is the most basic method.

 

First, let's look at the first classification. When an event handler knows that it will take a long time to execute an operation, you can call the QCoreApplication: processEvents () method, wait until all methods in the message queue are executed. Of course, this is the most basic method. It is only applicable and simple. If another program in the event queue calls this method, a deadlock occurs.

 

When you look at the time-sharing feature, it is suitable for decomposed operations (including serial and parallel operations). You only need to record the execution status of the current task and then execute it again. The procedure is as follows:

   1: Function EventHandler()

   2: {

3: // start timing

4: While (execution time <user-acceptable response time)

   5:     {

6: // perform the operation

   7:     }

8: // register the system idle event to continue processing

   9: }

To register system idle events in QT, you can use qtimer: singleshot (0, this, slot (calculate () to register the system idle signals in your own slot. Or use the qmetaobject: invokemethod (this, "Calculate", QT: queuedconnection); Method to asynchronously execute a method through invokemethod.

 

Finally, we will focus on how to reduce the response time. For data-intensive operations, we recommend using threadpool for management to reduce the thread context switching time. For Io-intensive operations, you can manage a thread by yourself, and this is also the scenario that I think the thread should be used most, that is, to make the CPU and peripherals fully run, reducing the total operation time.

 

For the reduction of the response time of parallel operations, the QT concurrent concept is introduced in QT, which adopts the MAP/reduce method. For details, refer to the concurrent programming section in QT.

 

Finally, I will explain how to change asynchronous operations to synchronous operations in QT. This is the content of QT special, which can be skipped by general readers.

The specific code is as follows:

   1: QNetworkAccessManager manager;

   2: QEventLoop q;

   3: QTimer tT;

   4:  

   5: tT.setSingleShot(true);

   6: connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));

   7: connect(&manager, SIGNAL(finished(QNetworkReply*)),

   8:         &q, SLOT(quit()));

   9: QNetworkReply *reply = manager.get(QNetworkRequest(QUrl("http://www.qtcentre.org")));

  10:  

  11: tT.start(5000); // 5s timeout

  12: q.exec();

  13:  

  14: if(tT.isActive()){

  15:     // download complete

  16:     tT.stop();

  17: } else {

  18:     // timeout

  19: }

The qeventloop class is used to create a local event loop and block it until it receives the finished signal or timeout signal, the event loop is not blocked.

 

Summary

This article analyzes the reasons that affect the GUI response speed. By comparing the task priority assignment method of the operating system, it explores the methods for multiple event types to improve the response speed, some of these methods are also common on other platforms.

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.