Question

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?

Was it helpful?

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.

OTHER TIPS

You can use any window in this scenario. Let's use QWidget:

  1. Subclass QWidget(let's name it MyWidget) and add whatever widgets you need as children to it.
  2. Add necessary slots to MyWidget which takes info from the outside(update progress bar, text labels etc.)
  3. Create another class(Calculator) which will do calculation(or whatever you need to do), and add necessary signals to it(pairs for slots you've declared before) and signal for quit

Then just do the following in your main:

QApplication app(...);
QThread thread;
MyWidget widget;
Calculator calculator;
//connect here your slots and signals, do not forget about quit signal to QWidget close() slot
calculator.moveToThread(&thread);
thread.start();
return app.exec();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top