QEventLoop and QT Event Loop

 

1. Generally, we are by the event loop exec () to open, such as the following example:

1 QCoreApplicaton::exec()
2 QApplication::exec()
3 QDialog::exec()
4 QThread::exec()
5 QDrag::exec()
6 QMenu::exec()

  These events are open loop, the event is first of all an infinite loop "recycling" program () inside an infinite loop in exec, make with the exec () code behind the lack of opportunity to run until the program () jump from exec. When () jump out from the exec, the event loop has been terminated. QEventLoop :: quit () can terminate the event loop.

  Event loop is actually similar to an event queue, the inclusion of the event is processed sequentially, and when done time and time is not the end of the cycle, which is actually more similar to a free CPU events for (;;) loop .

  Its essence is actually a way to re-queue allocation of time slices.

2. The event loop can be nested, when the sub-event loop, the parent event loop interrupt event is actually in the state, out of the loop when the child was the parent exec cycle events can be performed. Of course, this does not mean that in the implementation of the sub-cycle, similar to the parent loop interface response will be interrupted, because often there will be a majority in sub-cycle event a parent loop, execution QMessageBox :: exec (), QEventLoop :: exec () when, although the exec () interrupted main () in the QApplication :: exec (), but due to the response of the GUI interface it has been included in the sub-loop, so the GUI interface is still able to get a response.

3. If a sub-event loop is still valid, but the parent is forced to jump out of the cycle, when the parent out of the loop will not be executed immediately, but wait for the child after the event loop jump, the parent will be out of circulation

 

A few examples of it, for example, if you want the main thread to wait for 100ms, sleep can not use it, that would lead to a GUI interface to stop responding, but with the event loop to avoid this:

1 QEventLoop loop;
2 QTimer::singleShot(100, &loop, SLOT(quit()));
3 loop.exec();

There are, for example, a slot function, after the trigger will pop up a dialog, but write it like this, the window will be fleeting:

1 void ****::mySLot{
2     QDialog dlg;
3     dlg.show();
4 }

Of course, where you can use the dlg into a static member, to solve this issue through the lifetime of the growth period, but equally be used here eventLoop to solve this problem:

Copy the code
1 void ****::mySLot{
2     QDialog dlg;
3     dlg.show();
4     QEventLoop loop;
5     connect(&dlg, SIGNAL(finished(int)), &loop, SLOT(quit()));
6     loop.exec(QEventLoop::ExcludeUserInputEvents);
7 }
Copy the code

 

 

Turn: https://www.cnblogs.com/-wang-cheng/p/4973021.html

 

Guess you like

Origin www.cnblogs.com/xiangtingshen/p/11078332.html