PDA

View Full Version : Can I force my MainWindow to initialize its widgets before calling app.exec() ?



ComServant
17th June 2016, 01:43
In my project, I use alot of direct non-Qt OpenGL calls.

At some point during startup I want an opportunity to create/load some initial OpenGL resources. To do this, I need to call QOpenGLWidget::makeCurrent(),

My problem is, I can't call "QOpenGLWidget::makeCurrent()" until after application.exec() is entered. Is there some way I can force my QMainWindow's child QOpenGLWidgets to be initialized before application.exec() is entered?

=
MainWindow mainWindow(argc, argv);
if(!mainWindow.Initialize()) //I'd really like to load my OpenGL resources here...
{
return EXIT_FAILURE;
}

return application.exec(); //But QOpenGLWidget hasn't created the OpenGL context until this is entered.

anda_skoa
17th June 2016, 10:26
You can do some event processing by either calling QApplication::processEvents() or by using a local QEventLoop.

But delay something until the event loop runs, you can also call a slot via single shot timer or QMetaObject::invokeMethod().
Another technique is to do something the first time the main window's showEvent() is being called.

Cheers,
_

ComServant
18th June 2016, 06:43
Thank you! What I ended up doing is call qApp->processEvents() in mainWindow.Initialize(), like you suggest, and use a bool to test and return early in the overridden QOpenGLWidget::paintGL() function for any paint events that are triggered before mainWindow.Initialize() has actually finished.

I appreciate you taking the time to answer!