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:
- Function EventHandler ()
- {
- Start timing
- while (Execution time < user acceptable response time)
- {
- Perform action:}
- Registering System idle events to continue processing
- }
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:
- Qnetworkaccessmanager Manager;
- Qeventloop Q;
- Qtimer TT;
- Tt.setsingleshot (TRUE);
- Connect (&tt, SIGNAL (), &q, SLOT (Quit ()));
- Connect (&manager, SIGNAL (finished (qnetworkreply*)),
- &q, SLOT (Quit ()));
- Qnetworkreply *reply = Manager.get (qnetworkrequest (Qurl ("http://www.qtcentre.org"));
- Tt.start (5000); 5s Timeout
- Q.exec ();
- if (tt.isactive ()) {
- Download complete
- Tt.stop ();
- } else {
- Timeout
- }
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