PDA

View Full Version : Delay initializing?



MorrisLiang
23rd August 2010, 13:23
Here's my code:


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget myWidget;
return a.exec();
}

MyWidget::MyWidget()
{
...
show();
do_heavy_init();
}


As above, "a.exec();" is called after "do_heavy_init();".
Because "do_heavy_init()" costs much time, the main window would not show up immediately.
In my case, I actually want "do_heavy_init()" to be called after myWidget is shown up on the screen. Is there an elegant way to do so? Many thanks~

Zlatomir
23rd August 2010, 13:58
Why do you call show() in your class constructor and not in the main function?

And on topic, it depends on what do you want to initialize, give more information.

One idea is: add/encapsulate the data witch you initialize in and class, and do the initialization in that class constructor after you show the widget. Anyway give more info.

Lykurg
23rd August 2010, 14:13
Even if you do your heavy init after the widget is shown it will be useless since then your gui is shown but frozen. So if you can put all the stuff in a thread it would be the best.

MorrisLiang
23rd August 2010, 14:53
Why do you call show() in your class constructor and not in the main function?
And on topic, it depends on what do you want to initialize, give more information.

I think this is the same:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget myWidget;
myWidget.show();
return a.exec();
}

MyWidget::MyWidget()
{
...
do_heavy_init();
}
In do_heavy_init(), I detemine what content should be shown in the main window, before that, I can just show a loading symbol.


Even if you do your heavy init after the widget is shown it will be useless since then your gui is shown but frozen. So if you can put all the stuff in a thread it would be the best.
Yeah, it might freeze for a while. But what I want is to call do_heavy_init(); after myWidget is shown.


I just checked the QSplashScreen,
Can this ensure that myWidget is shown before do_heavy_init()? :


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget myWidget;
myWidget.show();

a.processEvents();
myWidget.do_heavy_init();
return a.exec();
}

Lykurg
23rd August 2010, 15:37
with a splash screen you can indicate that your application is loading, so it might fit your needs best. When the loading of your main application is ready, the splash is hidden and your main window is shown.
And in the splash screen you can show messages that the user is informed, what your application is doing.