Search code examples
qtqdialogqprogressbar

Launch window, process data, and close it without user interaction?


When the program first boots up, sometimes updating components will be required. The updating task should happen automatically, without any user interaction.

I want to show a simple window that will show the progress. When it'll be done, the window will be closed and the main window will launch.

I tried using a QDialog, and put the costly code in the init function, but of course it just blocked the window, and it didn't show up until it was already done.

There isn't a signal sent when the exec() runs, so I can't just start processing things just after executing the GUI window.

QProgressDialog may work here, but I actuaclly want to put there more than one progressbar.

Any ideas?


Solution

  • There are few aproaches. One of them is using QTimer to queue your process right after QDialog event loop start:

    QTimer::singleShot(0, this, SLOT(performUpdate());
    dlg.exec();
    

    Important thing from Qt docs:

    A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.

    So what happens here? We're scheduling slot performUpdate() to be executed as soon as control is back to event loop. When calling dlg.exec() you're starting new event loop. So your dialog will be shown first (as it is window system event) and then when everything get processed your slot will be executed.

    Worth to mention is that when you're performing blocking slot, you should call QApplication::processEvents() from time to time to get ui updated.