Qt: How to detect which version of OpenGL is being used?

大兔子大兔子 提交于 2020-01-29 13:11:05

问题


There are several ways that Qt can use OpenGL: desktop (native), ANGLE, ES... and now there's 'dynamic' which can choose at runtime. Within an app, is there a way that you can detect which one is in use? Either within C++ or within QML?

e.g. something equivalent to the global declarations that let you detect OS


回答1:


To detect the OpenGL version

  • in QML, there is OpenGLInfo
  • in C++, there is QOpenGLContext::openGLModuleType()
  • in the OpenGL C++ library, there is glGetString(GL_VERSION)
  • see also Qt blog article: Which OpenGL implementation is my Qt Quick app using today?

If you want to enforce a particular OpenGL version

  • set the option in software (see code example below)
    • for desktop/native, either set the environment variable QT_OPENGL to desktop or set the application attribute to Qt::AA_UseDesktopOpenGL
    • for ANGLE, either set the environment variable QT_OPENGL to angle or set the application attribute to Qt::AA_UseOpenGLES
    • for software rendering, either set the environment variable QT_OPENGL to software or set the application attribute to Qt::AA_UseSoftwareOpenGL
  • create a static build of Qt using the configure options to set the implementation of OpenGL you want (but be mindful of the Qt licensing rules)
    • for desktop/native, include -opengl desktop
    • for ANGLE, don't include an -opengl option; that's because it is the default
    • there is also -opengl dynamic which lets Qt choose the best option. This was introduced in Qt 5.4. If you want this option but don't need a static build for any other reason, there's no need to create a static build, as the prebuilt binaries use this option since Qt 5.5.
    • there are also other variants that you can explore at Qt for Windows - Requirements. Although this is a Windows-specific page, much of the information about configuring OpenGL for Qt is contained here. (Probably because most of the OpenGL rendering issues are on the Windows platform!)

Code example

#include <QGuiApplication>
//...

int main(int argc, char *argv[])
{
    // Set the OpenGL type before instantiating the application
    // In this example, we're forcing use of ANGLE.

    // Do either one of the following (not both). They are equivalent.
    qputenv("QT_OPENGL", "angle");
    QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);

    // Now instantiate the app
    QGuiApplication app(argc, argv);
    //...

    return app.exec();
}

(thanks to peppe for the initial answers in the comments above and thanks to user12345 for the Blog link)



来源:https://stackoverflow.com/questions/41021681/qt-how-to-detect-which-version-of-opengl-is-being-used

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!