Several Qt delay processing methods

In the process of embedded software development, "delay" is basically used. This article shares several Qt delay processing methods.

1. Blocking delay

The principle of blocking is: during the delay period, the event loop of this thread cannot be executed.

1. sleep() of QThread class

The simplest delay method is to use sleep(n), msleep(n), and usleep(n) of the QThread class. The undesirable consequences of these functions are that the GUI will lose response during the delay period and the interface will freeze. , Therefore, these three functions are generally used in non-GUI threads.

QThread::msleep(50);//阻塞延时50ms

2. Use timer: wait until death

voidDelay_MSec_Suspend(unsignedintmsec)
{
QTime_Timer=QTime::currentTime().addMSecs(msec);
while(QTime::currentTime()< _Timer );
}

2. Non-blocking delay

The principle is nothing more than using the event loop. There are two principles:

1. Process the event loop of this thread

While waiting, constantly force entry into the event loop of the current thread, so that all blocked events can be processed and the program will not get stuck.

voidDelay_MSec(unsignedintmsec)
{
QTime_Timer=QTime::currentTime().addMSecs(msec);
while(QTime::currentTime()< _Timer )
        QCoreApplication::AllEvents, 100);

2. Use sub-event loop

Create a child event loop. In the child event loop, the parent event loop is still executable.

voidDelay_MSec(unsignedintmsec)
{
QEventLooploop;//定义一个新的事件循环
QTimer::singleShot(msec,&loop,SLOT(quit()));//创建单次定时器,槽函数为事件循环的退出函数
loop.exec();//事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出
}

3. Processing of time-consuming code

Suppose there is such an application scenario: after clicking a button, an image needs to be read and processed, which takes 20 seconds to complete.

During these 20 seconds, the GUI will lose its effect and no elements on the interface can be clicked. What should be done in this situation? There are two methods: 1. Use another thread to handle this time-consuming task; 2. During the time-consuming task, continuously process the event loop of this thread to ensure timely response of the GUI.

for(i=0;i< 1000000; i++)
{
    //QCoreApplication::AllEvents);    //去处理本线程的事件循环,避免本线程被堵塞
    QCoreApplication::AllEvents, 5);//如果不够频繁,可以增加第二参数来缓解卡顿
 
    for(j=0;j< 1000000; j++)
    {
        //QCoreApplication::AllEvents);//处理事件循环,不建议放在这里,可能过于频繁
        doSomeThing();
    }
}

Generally speaking, processEvents() should not be called too frequently, nor should it be called not often enough. If it is too frequent, on the one hand, the response of the thread will be better, but on the other hand, it will cause the already time-consuming task to become more time-consuming; if it is not frequent enough, it may obviously make the response of the GUI thread worse, for example, every 500ms is only called once, then the GUI event loop can only be processed once every 500ms. Of course, this problem can be slightly alleviated by setting the second formal parameter of processEvents(). A better approach is to ensure that the called The period is <200ms (smaller is better, depending on the program requirements), so that no lag is visible to the naked eye.

Side effects: (Pay special attention!)

1. After clicking the button, this 20s time-consuming task starts to be executed. Before it is completed, we click the close button of the GUI, then the GUI will disappear immediately, but this time-consuming task will still be executed in the background until it is completed. The process will exit. Solution: Rewrite the shutdown event and end the process directly in the function of the shutdown event.

2. After clicking the button, this 20s time-consuming task begins to execute. When the execution reaches the 5th second, we click the button again, then QT will execute a new 20s task. After this new task is completed, it will continue Continue the execution of the first 20s task from the 5th second when it was last interrupted. If the task is reentrant, the result is just that it is executed twice. If the task is not reentrant, the situation is completely bad. Solution: Disable the button after clicking it, and then enable it after execution.

As a benefit for this article, you can receive a Qt development learning package and technical videos for free , including (Qt practical projects, C++ language basics, C++ design patterns, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project practice, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click at the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/133205248