Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qt

opengl

qt5

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

like image 897
Paul Masri-Stone Avatar asked Dec 07 '16 15:12

Paul Masri-Stone


1 Answers

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)

like image 113
Paul Masri-Stone Avatar answered Sep 28 '22 02:09

Paul Masri-Stone