【发布时间】:2023-04-08 15:58:01
【问题描述】:
我正在 Windows 上开发一个大量使用 OpenGL 的 Qt 项目。它最初配置为使用 OpenGL 2.1 版,一切正常。最近,我将代码中的OpenGL版本升级到了3.0。现在,项目在初始化期间很早就崩溃了,并出现以下错误:
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
...这是调试器停止的行:
if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
在 global.cpp 中,它在该块的末尾失败。具体来说,错误信息中给出的行号对应于#endif这一行:
#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
查看 qopenglcontext.cpp,第 1238 行实际上是在一个大的注释块中。下面是直接跟随的代码,根据上面的错误消息(Q_ASSERT(qGuiApp) 行),几乎可以肯定这是正确的位置:
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
}
请记住,这些是标准 Qt 文件,这是我从未接触过的代码。
这是我迄今为止尝试过的总结。当然,这些都不起作用:
- 尝试设置我的环境变量,如下页所示:http://doc.qt.io/qt-5/windows-requirements.html。具体来说,我将我的 QT_OPENGL var 设置为桌面、角度和软件,但没有任何效果。
- 按照同一页面的建议,我尝试在我的 .pro 文件中添加以下行:“LIBS += opengl32.lib”和“LIBS += -lopengl32”(我添加了一行,对其进行了测试,将其删除,然后添加了第二行。)
- 尝试取消设置 QT_OPENGL_DYNAMIC 变量。
- 下载并运行 OpenGL Extensions Viewer 4.1 并确认我的 OpenGL 存在并且设置得很好。渲染测试功能证明 OpenGL 在我的系统上正常运行。
- 还成功运行了 Qt 内置的 HelloWorld OpenGL 示例。这运行良好,确认 Qt 能够以其他方式运行 OpenGL。
我在这里没有想法。欢迎任何建议或知识。另外,如果您需要更多信息,请询问,我会及时回复。
编辑:这是主要的开始:
int main(int argc, char* argv[])
{
// setup OpenGL before application initialization
GLFunctions::setupOpenGL();
QApplication app(argc, argv);
....
}
这里是设置函数:
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);
}
}
【问题讨论】:
-
你只是没有通过断言。无论 qGuiApp 是什么,它都是空的。它在哪里初始化?
-
这是我的 main.cpp 的开头: int main(int argc, char* argv[]) { // 在应用程序初始化之前设置 OpenGL GLFunctions::setupOpenGL(); QApplication 应用程序(argc,argv); ...
-
这是我的 setupOpenGL 函数: 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); } 其他 { fmt.setVersion(3, 0);正如我原来的帖子所指出的,它在 if 语句上失败了。抱歉格式化,我不明白如何在这样的 cmets 中格式化代码。
-
您在 Qt 之前设置 OpenGL,但在 openGL 设置中使用 qGuiApp。所以 qGuiApp 将为空。
-
想想
qGuiApp是什么,为什么在你看到它为空的时候它是空的,以及它与你的代码有何关系。
标签: c++ windows qt opengl opengl-es