Qt QT fail to create OpenGL context for format QSurfaceFormat

时间:2024-02-19 12:43:10

在编写QML代码的时候有时候会出现以下报错,有时候又不出现,莫名其妙,后来找到了解决方法

解决方式:在main.cpp中添加系列语句

    qputenv("QT_OPENGL", "software");

 添加以上代码如果也没有明显改善的话,请注意报错之后控制台输出的信息

ARB::createContext: wglCreateContextAttribsARB() failed (GL error code: 0x0) for format: QSurfaceFormat(version 2.0, options QFlags<QSurfaceFormat::FormatOption>(), depthBufferSize 24, redBufferSize -1, greenBufferSize -1, blueBufferSize -1, alphaBufferSize -1, stencilBufferSize 8, samples -1, swapBehavior QSurfaceFormat::DoubleBuffer, swapInterval 1, colorSpace QSurfaceFormat::DefaultColorSpace, profile  QSurfaceFormat::NoProfile), shared context: 0x0 (操作成功完成。)
GDI::createContext: wglCreateContext failed. (句柄无效。)
Unable to create a GL Context.
Failed to create OpenGL context for format QSurfaceFormat(version 2.0, options QFlags<QSurfaceFormat::FormatOption>(), depthBufferSize 24, redBufferSize -1, greenBufferSize -1, blueBufferSize -1, alphaBufferSize -1, stencilBufferSize 8, samples -1, swapBehavior QSurfaceFormat::DoubleBuffer, swapInterval 1, colorSpace QSurfaceFormat::DefaultColorSpace, profile  QSurfaceFormat::NoProfile) .
This is most likely caused by not having the necessary graphics drivers installed.

Install a driver providing OpenGL 2.0 or higher, or, if this is not possible, make sure the ANGLE Open GL ES 2.0 emulation libraries (libEGLd.dll, libGLESv2d.dll and d3dcompiler_*.dll) are available in the application executable\'s directory or in a location listed in PATH.

请注意上面最后一句话,说明报错的原因是因为OpenGL版本过低的原因,可以按照这一行提示的方法解决这个问题。

由于我的电脑有分为集显和核显,之前报错可能就是因为集显OpenGL版本过低。

在NVIDA控制面板中,将QT首选图形处理器设置为核显

之后再运行程序便没有问题了。