How to wait for QProgressDialog construction?

477 views Asked by At

Before long operation is executed, I'm invoking QProgressDialog to inform user about the progress of the execution. The long operations is waiting for thread to be terminated.

The problem is, when I use show method of QProgressDialog, the progress dialog is not fully constructed and the thread goes into the wait method of the thread, as you can see on the figure below:

1

Meanwhile when I use exec method of the QProgressDialog, the progress dialog is fully constructed, but the wait method is not invoked until I exit the progress dialog, as you can see here:

2

This is the code I'm using:

auto dialog = new QProgressDialog();
dialog->setWindowTitle("Wait");
dialog->setLabelText("Aborting optimization");

dialog->resize(100, 30);
dialog->show();

optimizerUi->terminate();
optimizerUi->wait();

dialog->hide();

Is there any way I can wait for or force the progress dialog to be fully constructed before the thread falls into the wait method, which takes quite a long time and blocks the main thread? Thank you for any suggestion.

1

There are 1 answers

0
Minh On BEST ANSWER

You can wait for the thread to finish with some timeout. If the thread is finished, close the progress dialog. If it isn't, process the application events.

auto dialog = new QProgressDialog();
dialog->setWindowTitle("Wait");
dialog->setRange(0, 0);
dialog->setLabelText("Aborting optimization");
dialog->setCancelButton(nullptr);

dialog->resize(100, 30);
dialog->show();

optimizerUi->terminate();
while (!optimizerUi->wait(QDeadlineTimer{100}))
{
    QApplication::processEvents();
}
dialog->hide();