This is an example demonstrating how C++20 coroutines can be used for asynchronous work with a non-modal dialog in a simple GUI application. This example has been tested with Qt 5 and gcc 12.2.0. The main window has a button "Get text" and a line edit. On the button click, a non-modal dialog is showed, where you can type some text and click "Ok". After that, the dialog closes, and the text typed appears in the line edit control of the main window.
The essence of the example is the handler of clicking the "Get text" button:
coro MainWnd::onGetTextClicked() {
QString text = co_await m_dlg.getText();
m_pOutput->setText(text);
}It is a coroutine which shows the dialog and suspends, after that the GUI thread proceeds the message queue handling. You can work with the main window, enter some text into its line edit control, even click the "Get text" button again. The coroutine resumes when you click the "Ok" button in the dialog and sets the new content to the main window line edit control. Meanwhile, the code looks almost like a simple synchronous code launching a blocking modal dialog. Note that the example is single-threaded despite being asynchronous.
NonModalCoro$ mkdir build
NonModalCoro$ cd build
NonModalCoro/build$ cmake ..
NonModalCoro/build$ make