问题
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
todesktop
or set the application attribute toQt::AA_UseDesktopOpenGL
- for ANGLE, either set the environment variable
QT_OPENGL
toangle
or set the application attribute toQt::AA_UseOpenGLES
- for software rendering, either set the environment variable
QT_OPENGL
tosoftware
or set the application attribute toQt::AA_UseSoftwareOpenGL
- for desktop/native, either set the environment variable
- 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!)
- for desktop/native, include
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