The hellogles3 sample constructs the QGuiApplication
before testing for desktop OpenGL. If you don't do this then QOpenGLContext::openGLModuleType()
crashes.
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QSurfaceFormat fmt;
fmt.setDepthBufferSize(24);
// Request OpenGL 3.3 core or OpenGL ES 3.0.
if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
qDebug("Requesting 3.3 core context");
fmt.setVersion(3, 3);
fmt.setProfile(QSurfaceFormat::CoreProfile);
} else {
qDebug("Requesting 3.0 context");
fmt.setVersion(3, 0);
}
QSurfaceFormat::setDefaultFormat(fmt);
GLWindow glWindow;
glWindow.showMaximized();
return app.exec();
}
However, if you are using Qt::AA_ShareOpenGLContexts
, then QSurfaceFormat::setDefaultFormat must be called before constructing QGuiApplication
:
Note: When setting Qt::AA_ShareOpenGLContexts, it is strongly recommended to place the call to this function before the construction of the QGuiApplication or QApplication. Otherwise format will not be applied to the global share context and therefore issues may arise with context sharing afterwards.
Attempting to call create on a standalone QOpenGLContext
instance to test OpenGL type before constructing QGuiApplication
also crashes, because QGuiApplicationPrivate::platformIntegration()
has not been created yet.
Is there any way around this?