在編寫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首選圖形處理器設置為核顯
之后再運行程序便沒有問題了。