0
votes

I'm working on a Qt project on Windows that heavily uses OpenGL. It was originally configured to use OpenGL version 2.1, and everything worked fine. Recently, I upgraded the OpenGL version in the code to 3.0. Now, the project crashes very early during initialization with the following error:

QML debugging is enabled. Only use this in a safe environment.
ASSERT: "qGuiApp" in file kernel\qopenglcontext.cpp, line 1238
Debug Error!

Program: C:\Qt\5.7\msvc2015_64\bin\Qt5Cored.dll
Module: 5.7.0
File: global\qglobal.cpp
Line: 3063

ASSERT: "qGuiApp" in file kernel\qopenglcontext.cpp, line 1238

... and this is the line that the debugger stops on:

if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {

In global.cpp, it's failing at the end of this block. Specifically, the line number given in the error message corresponds to the #endif line:

#ifndef QT_NO_EXCEPTIONS
/*
   \internal
   Allows you to call std::terminate() without including <exception>.
   Called internally from QT_TERMINATE_ON_EXCEPTION
*/
Q_NORETURN void qTerminate() Q_DECL_NOTHROW
{
    std::terminate();
}
#endif

Looking at qopenglcontext.cpp, line 1238 is actually within a large comment block. Here is the code that directly follows, which is almost certainly the right place based on the error message above (the Q_ASSERT(qGuiApp) line):

QOpenGLContext::OpenGLModuleType QOpenGLContext::openGLModuleType()
{
#if defined(QT_OPENGL_DYNAMIC)
    Q_ASSERT(qGuiApp);
    return QGuiApplicationPrivate::instance()->platformIntegration()->openGLModuleType();
#elif defined(QT_OPENGL_ES_2)
    return LibGLES;
#else
    return LibGL;
#endif 
}

Keep in mind these are standard Qt files and this is code I have never touched.

Here is a summary of what I've tried so far. Of course, none of these worked:

  • Tried setting my environment variables like the following page suggests: http://doc.qt.io/qt-5/windows-requirements.html. Specifically, I set my QT_OPENGL var to desktop, angle, and then software, and nothing worked.
  • As the same page suggested, I tried adding the following lines to my .pro file: "LIBS += opengl32.lib" and "LIBS += -lopengl32" (I added one line, tested it, removed it, then added the second line.)
  • Tried un-setting the QT_OPENGL_DYNAMIC variable.
  • Downloaded and ran OpenGL Extensions Viewer 4.1 and confirmed that my OpenGL exists and is setup just fine. The Render Test functionality proves that OpenGL is otherwise working on my system.
  • Also successfully ran the HelloWorld OpenGL example that's built-in to Qt. This ran fine, confirming that Qt is otherwise able to run OpenGL.

I'm out of ideas here. Any suggestions or knowledge would be welcome. Also, if you need more information please ask and I will respond promptly.

EDIT : Here is the beginning of main:

int main(int argc, char* argv[]) 
{ 
   // setup OpenGL before application initialization 
   GLFunctions::setupOpenGL(); 
   QApplication app(argc, argv);
   ....
}

And here is the setup function:

static void setupOpenGL()
{
    QSurfaceFormat fmt; 
    fmt.setDepthBufferSize( 24 ); 
    if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) 
    { 
        fmt.setVersion(3, 3); 
        fmt.setProfile(QSurfaceFormat::CoreProfile); 
        //fmt.setRenderableType(QSurfaceFormat::OpenGL); 
    } 
    else 
    { 
        fmt.setVersion(3, 0); 
    }
}
1
You're just failing the assert. Whatever qGuiApp is, it's null. Where is it getting initialized? - picklechips
Here's the beginning of my main.cpp: int main(int argc, char* argv[]) { // setup OpenGL before application initialization GLFunctions::setupOpenGL(); QApplication app(argc, argv); ... - user1765354
And here's my setupOpenGL function: static void setupOpenGL(){ QSurfaceFormat fmt; fmt.setDepthBufferSize( 24 ); if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { fmt.setVersion(3, 3); fmt.setProfile(QSurfaceFormat::CoreProfile); //fmt.setRenderableType(QSurfaceFormat::OpenGL); } else { fmt.setVersion(3, 0); } As my original post indicated, it's failing on the if-statement. Sorry about formatting, I don't understand how to format code in comments like this. - user1765354
You're setting up OpenGL before Qt, but using qGuiApp in the openGL setup. So qGuiApp will be null. - picklechips
Think about what qGuiApp is, and why is it null at the point you see it being null, and how it relates to your code. - Kuba hasn't forgotten Monica

1 Answers

1
votes

From the documentation of QOpenGLContext::openGLModuleType():

Note: This function requires that the QGuiApplication instance is already created.

You may set your desired version regardless of the openGLModuleType (remove the check) and check later if you got your requested version or not.