QGLFunctions Class

The QGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API. More...

Header: #include <QGLFunctions>
qmake: QT += opengl
Since: Qt 4.8

This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

This class was introduced in Qt 4.8.

Public Types

enum OpenGLFeature { Multitexture, Shaders, Buffers, Framebuffers, ..., NPOTTextures }
flags OpenGLFeatures

Public Functions

QGLFunctions(const QGLContext *context)
QGLFunctions()
bool hasOpenGLFeature(QGLFunctions::OpenGLFeature feature) const
void initializeGLFunctions(const QGLContext *context = nullptr)
QGLFunctions::OpenGLFeatures openGLFeatures() const

Detailed Description

OpenGL ES 2.0 defines a subset of the OpenGL specification that is common across many desktop and embedded OpenGL implementations. However, it can be difficult to use the functions from that subset because they need to be resolved manually on desktop systems.

QGLFunctions provides a guaranteed API that is available on all OpenGL systems and takes care of function resolution on systems that need it. The recommended way to use QGLFunctions is by direct inheritance:


      class MyGLWidget : public QGLWidget, protected QGLFunctions
      {
          Q_OBJECT
      public:
          MyGLWidget(QWidget *parent = 0) : QGLWidget(parent) {}

      protected:
          void initializeGL();
          void paintGL();
      };

      void MyGLWidget::initializeGL()
      {
          initializeGLFunctions();
      }

The paintGL() function can then use any of the OpenGL ES 2.0 functions without explicit resolution, such as glActiveTexture() in the following example:


      void MyGLWidget::paintGL()
      {
          glActiveTexture(GL_TEXTURE1);
          glBindTexture(GL_TEXTURE_2D, textureId);
          ...
      }

QGLFunctions can also be used directly for ad-hoc invocation of OpenGL ES 2.0 functions on all platforms:


      QGLFunctions glFuncs(QGLContext::currentContext());
      glFuncs.glActiveTexture(GL_TEXTURE1);

QGLFunctions provides wrappers for all OpenGL ES 2.0 functions, except those like glDrawArrays(), glViewport(), and glBindTexture() that don't have portability issues.

Including the header for QGLFunctions will also define all of the OpenGL ES 2.0 macro constants that are not already defined by the system's OpenGL headers, such as GL_TEXTURE1 above.

The hasOpenGLFeature() and openGLFeatures() functions can be used to determine if the OpenGL implementation has a major OpenGL ES 2.0 feature. For example, the following checks if non power of two textures are available:


      QGLFunctions funcs(QGLContext::currentContext());
      bool npot = funcs.hasOpenGLFeature(QGLFunctions::NPOTTextures);

Note: This class has been deprecated in favor of QOpenGLFunctions.

Member Type Documentation

enum QGLFunctions::OpenGLFeature
flags QGLFunctions::OpenGLFeatures

This enum defines OpenGL ES 2.0 features that may be optional on other platforms.

ConstantValueDescription
QGLFunctions::Multitexture0x0001glActiveTexture() function is available.
QGLFunctions::Shaders0x0002Shader functions are available.
QGLFunctions::Buffers0x0004Vertex and index buffer functions are available.
QGLFunctions::Framebuffers0x0008Framebuffer object functions are available.
QGLFunctions::BlendColor0x0010glBlendColor() is available.
QGLFunctions::BlendEquation0x0020glBlendEquation() is available.
QGLFunctions::BlendEquationSeparate0x0040glBlendEquationSeparate() is available.
QGLFunctions::BlendFuncSeparate0x0080glBlendFuncSeparate() is available.
QGLFunctions::BlendSubtract0x0100Blend subtract mode is available.
QGLFunctions::CompressedTextures0x0200Compressed texture functions are available.
QGLFunctions::Multisample0x0400glSampleCoverage() function is available.
QGLFunctions::StencilSeparate0x0800Separate stencil functions are available.
QGLFunctions::NPOTTextures0x1000Non power of two textures are available.

The OpenGLFeatures type is a typedef for QFlags<OpenGLFeature>. It stores an OR combination of OpenGLFeature values.

Member Function Documentation

QGLFunctions::QGLFunctions(const QGLContext *context)

Constructs a function resolver for context. If context is nullptr, then the resolver will be created for the current QGLContext.

An object constructed in this way can only be used with context and other contexts that share with it. Use initializeGLFunctions() to change the object's context association.

See also initializeGLFunctions().

QGLFunctions::QGLFunctions()

Constructs a default function resolver. The resolver cannot be used until initializeGLFunctions() is called to specify the context.

See also initializeGLFunctions().

bool QGLFunctions::hasOpenGLFeature(QGLFunctions::OpenGLFeature feature) const

Returns true if feature is present on this system's OpenGL implementation; false otherwise.

It is assumed that the QGLContext associated with this function resolver is current.

See also openGLFeatures().

void QGLFunctions::initializeGLFunctions(const QGLContext *context = nullptr)

Initializes GL function resolution for context. If context is nullptr, then the current QGLContext will be used.

After calling this function, the QGLFunctions object can only be used with context and other contexts that share with it. Call initializeGLFunctions() again to change the object's context association.

QGLFunctions::OpenGLFeatures QGLFunctions::openGLFeatures() const

Returns the set of features that are present on this system's OpenGL implementation.

It is assumed that the QGLContext associated with this function resolver is current.

See also hasOpenGLFeature().