In the Qt platform, keeping the GUI responsive is what we will introduce in this article.Qt PlatformInGUIKeep response smooth? 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 that it can provide program running speed. Otherwise, improper use of threads often slows down the program.QT PlatformIs it possible to increase the response speed through methods other than multithreading? This article will help you solve the problem.
First, what isGUI? A:GUIThe response is that the systemGUIEvent processing speed.
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:
- FunctionEventHandler ()
- {
- // Start timing
- While (execution time <user-acceptable response time)
- {
- // Perform the following operations :}
- // Register idle system events to continue processing
- }
InQT PlatformTo register system idle events, 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.
To reduce the response time of parallel operationsQT PlatformIntroduced the concept of QtConcurrent, using the Map/Reduce method, For details, refer to the ConcurrentProgramming section in the QT platform.
Finally, I will explain howQT PlatformChange asynchronous operations to synchronous operations. This is the content of QTspecial, which can be skipped by general readers.
The specific code is as follows:
- QNetworkAccessManagermanager;
- QEventLoopq;
- QTimertT;
- tT.setSingleShot(true);
- connect(&tT,SIGNAL(timeout()),&q,SLOT(quit()));
- connect(&manager,SIGNAL(finished(QNetworkReply*)),
- &q,SLOT(quit()));
- QNetworkReply*reply=manager.get(QNetworkRequest(QUrl("http://www.qtcentre.org")));
- tT.start(5000);//5stimeout
- q.exec();
- if(tT.isActive()){
- //downloadcomplete
- tT.stop();
- }else{
- //timeout
- }
The QEventLoop class is mainly used. It creates a local Eventloop and blocks it until it receives the finished signal or timeout signal. The event loop is not blocked.
Summary: How to makeQt PlatformMediumGUIWe hope that this article will help you with your smooth response!