The Qt platform keeps the GUI in a smooth response

Source: Internet
Author: User

How to keep the GUI in the Qt platform in a smooth response? Generally time-consuming operations are divided into compute-intensive operations and IO-intensive operations to improve response times for these two types of operations.

And from the operational nature, the operation can be divided into non-decomposition operations, such as in the third-party library time-consuming operations, as well as the decomposition of operations, which can be broken down into serial operations and parallel operations, how to improve the response speed for these kinds of operations?

How can asynchronous operations, such as asynchronous read data methods in a network library, become synchronous operations?

In addition, for multi-threading, the general view is that you can provide the speed of the program, in fact, the improper use of the thread often makes the program slow, so in QT can be outside the multi-threading method to improve the response speed? This article is for you to solve.

First, what is the response to the GUI ? A: TheGUI response is the system's processing speed for GUI events.

Because the system handles events that take a certain amount of time, the General Window System provides an event queue to store events. If each event is treated as a task, then event handling is similar to the operating system handling the task by priority, which minimizes the average wait time for each task. Then you can draw on the methods in the operating system, such as:

Ratings, allowing longer tasks to be deferred for execution.

Tick, for a longer task, let it execute for a period of time after pausing and then executing.

Reducing the time it takes to run each task is, of course, the most basic approach.

Look at the first rating, and when an event handler knows that it will take a lengthy operation, it can call Qcoreapplication::p the Rocessevents () method, waiting for the methods in the message queue to execute. This is, of course, the most basic approach, and only works with simple cases where a deadlock occurs if another program in the event queue also calls the method.

Again, it applies to the decomposition of operations (both serial and parallel) by simply documenting the execution of the current task and then executing it again. Its use flow is as follows:

    1. Function EventHandler ()
    2. {
    3. Start timing
    4. while (Execution time < user acceptable response time)
    5. {
    6. Perform action:}
    7. Registering System idle events to continue processing
    8. }

The method of registering the System idle event in QT can be done by Qtimer::singleshot (0, this, slot (calculate ())), and registering the System idle signal in its own slot. or use Qmetaobject::invokemethod (this, "calculate", Qt::queuedconnection), and execute a method asynchronously by InvokeMethod.

Finally, the focus is on how to reduce the response time, for data-intensive operations, it is recommended to use ThreadPool to manage, reduce the time of thread context switching, and for IO-intensive operations, you manage a thread to implement, and this is what I think the thread most should use the scenario, That allows the CPU and peripherals to operate at full capacity, reducing total operating time.

For the reduction of the response time of parallel operation, the concept of QT concurrent is introduced in QT , and the Map/reduce method can be used to refer to the Concurrent Programming section in QT.

Finally, how to change the asynchronous operation to synchronous operation in Qt , this belongs to the content of QT Special, the general reader can skip.

The specific code is as follows:

  1. Qnetworkaccessmanager Manager;
  2. Qeventloop Q;
  3. Qtimer TT;
  4. Tt.setsingleshot (TRUE);
  5. Connect (&tt, SIGNAL (), &q, SLOT (Quit ()));
  6. Connect (&manager, SIGNAL (finished (qnetworkreply*)),
  7. &q, SLOT (Quit ()));
  8. Qnetworkreply *reply = Manager.get (qnetworkrequest (Qurl ("http://www.qtcentre.org"));
  9. Tt.start (5000); 5s Timeout
  10. Q.exec ();
  11. if (tt.isactive ()) {
  12. Download complete
  13. Tt.stop ();
  14. } else {
  15. Timeout
  16. }

The main use of the Qeventloop class, it will create a local event loop, and then block, until the finished signal is received, or timeout time-out signal before exiting, and the event loop will not be block.

The Qt platform keeps the GUI in a smooth response

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.