Processing of time-consuming operations on the Qt main thread: processEvents() function

When the Qt main thread processes time-consuming operations, the interface is in a "fake death" state due to too long a time, and no longer responds to other operations; The easiest way to use multithreading is to use QApplication::processEvents().

void QCoreApplication::processEvents ( QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents ) [static]

Qt Assistant Explanation: Processes all pending events for the calling thread according to the specified flags until there are no more events to process. This function can be called occasionally when the program is busy performing a long operation such as copying a file. If a local loop that continuously calls this function is running, without an event loop, the DeferredDelete event will not be processed. This may affect the behavior of widgets (such as QToolTip) that rely on the DeferredDelete event to function properly. Another way is to call sendPostedEvents() from a local loop.
Calling this function handles events only for the calling thread.
Note: This function is thread safe.

Other applications: non-blocking delay

QTime time;
time.start();
while(time.elapsed() < 1000)   //等待时间1秒钟
{
    
    
	QCoreApplication::processEvents();   //不停地处理事件,让程序保持响应
}             
    

Guess you like

Origin blog.csdn.net/yulong_abc/article/details/128985907