|  | Home · All Classes · Modules

# QGLContext Class Reference
[QtOpenGL module]

The QGLContext class encapsulates an OpenGL rendering context. More...

### Methods

- __init__ (self, QGLFormat format, QPaintDevice device)
- unsigned long bindTexture (self, QImage image, unsigned long target = GL_TEXTURE_2D, int format = GL_RGBA8)
- unsigned long bindTexture (self, QPixmap pixmap, unsigned long target = GL_TEXTURE_2D, int format = GL_RGBA8)
- unsigned long bindTexture (self, QString fileName)
- bool chooseContext (self, QGLContext shareContext = None)
- sip.voidptr chooseVisual (self)
- bool create (self, QGLContext shareContext = None)
- deleteTexture (self, unsigned long tx_id)
- QPaintDevice device (self)
- bool deviceIsPixmap (self)
- doneCurrent (self)
- drawTexture (self, QRectF target, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)
- drawTexture (self, QPointF point, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)
- QGLFormat format (self)
- generateFontDisplayLists (self, QFont fnt, int listBase)
- sip.voidptr getProcAddress (self, QString proc)
- bool initialized (self)
- bool isSharing (self)
- bool isValid (self)
- makeCurrent (self)
- QColor overlayTransparentColor (self)
- QGLFormat requestedFormat (self)
- reset (self)
- setFormat (self, QGLFormat format)
- setInitialized (self, bool on)
- setWindowCreated (self, bool on)
- swapBuffers (self)
- bool windowCreated (self)

### Static Methods

- QGLContext currentContext ()
- setTextureCacheLimit (int size)
- int textureCacheLimit ()

## Detailed Description

The QGLContext class encapsulates an OpenGL rendering context.

An OpenGL rendering context is a complete set of OpenGL state variables. The rendering context's format is set in the constructor, but it can also be set later with setFormat(). The format options that are actually set are returned by format(); the options you asked for are returned by requestedFormat(). Note that after a QGLContext object has been constructed, the actual OpenGL context must be created by explicitly calling the create() function. The makeCurrent() function makes this context the current rendering context. You can make no context current using doneCurrent(). The reset() function will reset the context and make it invalid.

You can examine properties of the context with, e.g. isValid(), isSharing(), initialized(), windowCreated() and overlayTransparentColor().

If you're using double buffering you can swap the screen contents with the off-screen buffer using swapBuffers().

Please note that QGLContext is not thread safe.

## Method Documentation

### QGLContext.__init__ (self, QGLFormat format, QPaintDevice device)

Constructs an OpenGL context with the given format which specifies several display options for the context.

If the underlying OpenGL/Window system cannot satisfy all the features requested in format, the nearest subset of features will be used. After creation, the format() method will return the actual format obtained.

Note that after a QGLContext object has been constructed, create() must be called explicitly to create the actual OpenGL context. The context will be invalid if it was not possible to obtain a GL context at all.

See also format() and isValid().

### unsigned long QGLContext.bindTexture (self, QImage image, unsigned long target = GL_TEXTURE_2D, int format = GL_RGBA8)

Generates and binds a 2D GL texture to the current context, based on image. The generated texture id is returned and can be used in later `glBindTexture()` calls.

The target parameter specifies the texture target. The default target is `GL_TEXTURE_2D`.

The format parameter sets the internal format for the texture. The default format is `GL_RGBA8`.

If the GL implementation supports the `GL_SGIS_generate_mipmap` extension, mipmaps will be automatically generated for the texture. Mipmap generation is only supported for the `GL_TEXTURE_2D` target.

The texture that is generated is cached, so multiple calls to bindTexture() with the same QImage will return the same texture id.

Note that we assume default values for the glPixelStore() and glPixelTransfer() parameters.

See also deleteTexture().

### unsigned long QGLContext.bindTexture (self, QPixmap pixmap, unsigned long target = GL_TEXTURE_2D, int format = GL_RGBA8)

This is an overloaded member function, provided for convenience.

Reads the DirectDrawSurface (DDS) compressed file fileName and generates a 2D GL texture from it.

Only the DXT1, DXT3 and DXT5 DDS formats are supported.

Note that this will only work if the implementation supports the `GL_ARB_texture_compression` and `GL_EXT_texture_compression_s3tc` extensions.

See also deleteTexture().

### unsigned long QGLContext.bindTexture (self, QString fileName)

This is an overloaded member function, provided for convenience.

Generates and binds a 2D GL texture based on pixmap.

### bool QGLContext.chooseContext (self, QGLContext shareContext = None)

This semi-internal function is called by create(). It creates a system-dependent OpenGL handle that matches the format() of shareContext as closely as possible, returning true if successful or false if a suitable handle could not be found.

On Windows, it calls the virtual function choosePixelFormat(), which finds a matching pixel format identifier. On X11, it calls the virtual function chooseVisual() which finds an appropriate X visual. On other platforms it may work differently.

### sip.voidptr QGLContext.chooseVisual (self)

X11 only: This virtual function tries to find a visual that matches the format, reducing the demands if the original request cannot be met.

The algorithm for reducing the demands of the format is quite simple-minded, so override this method in your subclass if your application has spcific requirements on visual selection.

See also chooseContext().

### bool QGLContext.create (self, QGLContext shareContext = None)

Creates the GL context. Returns true if it was successful in creating a valid GL rendering context on the paint device specified in the constructor; otherwise returns false (i.e. the context is invalid).

After successful creation, format() returns the set of features of the created GL rendering context.

If shareContext points to a valid QGLContext, this method will try to establish OpenGL display list and texture object sharing between this context and the shareContext. Note that this may fail if the two contexts have different formats. Use isSharing() to see if sharing is in effect.

Warning: Implementation note: initialization of C++ class members usually takes place in the class constructor. QGLContext is an exception because it must be simple to customize. The virtual functions chooseContext() (and chooseVisual() for X11) can be reimplemented in a subclass to select a particular context. The problem is that virtual functions are not properly called during construction (even though this is correct C++) because C++ constructs class hierarchies from the bottom up. For this reason we need a create() function.

See also chooseContext(), format(), and isValid().

### QGLContext QGLContext.currentContext ()

Returns the current context, i.e. the context to which any OpenGL commands will currently be directed. Returns 0 if no context is current.

See also makeCurrent().

### QGLContext.deleteTexture (self, unsigned long tx_id)

Removes the texture identified by id from the texture cache. If the context is not shared by any other QGLContext, glDeleteTextures() will be called to delete the texture from the context.

See also bindTexture().

### QPaintDevice QGLContext.device (self)

Returns the paint device set for this context.

See also QGLContext.QGLContext().

### bool QGLContext.deviceIsPixmap (self)

Returns true if the paint device of this context is a pixmap; otherwise returns false.

### QGLContext.doneCurrent (self)

Makes no GL context the current context. Normally, you do not need to call this function; QGLContext calls it as necessary.

### QGLContext.drawTexture (self, QRectF target, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)

Draws the given texture, textureId, to the given target rectangle, target, in OpenGL model space. The textureTarget should be a 2D texture target.

Equivalent to the corresponding QGLContext.drawTexture().

This function was introduced in Qt 4.4.

### QGLContext.drawTexture (self, QPointF point, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)

This is an overloaded member function, provided for convenience.

Draws the given texture at the given point in OpenGL model space. The textureTarget should be a 2D texture target.

Equivalent to the corresponding QGLContext.drawTexture().

This function was introduced in Qt 4.4.

### QGLFormat QGLContext.format (self)

Returns the frame buffer format that was obtained (this may be a subset of what was requested).

See also setFormat() and requestedFormat().

### QGLContext.generateFontDisplayLists (self, QFont fnt, int listBase)

### sip.voidptr QGLContext.getProcAddress (self, QString proc)

Returns a function pointer to the GL extension function passed in proc. 0 is returned if a pointer to the function could not be obtained.

### bool QGLContext.initialized (self)

Returns true if this context has been initialized, i.e. if QGLWidget.initializeGL() has been performed on it; otherwise returns false.

See also setInitialized().

### bool QGLContext.isSharing (self)

Returns true if this context is sharing its GL context with another QGLContext, otherwise false is returned. Note that context sharing might not be supported between contexts with different formats.

### bool QGLContext.isValid (self)

Returns true if a GL rendering context has been successfully created; otherwise returns false.

### QGLContext.makeCurrent (self)

Makes this context the current OpenGL rendering context. All GL functions you call operate on this context until another context is made current.

In some very rare cases the underlying call may fail. If this occurs an error message is output to stderr.

### QColor QGLContext.overlayTransparentColor (self)

If this context is a valid context in an overlay plane, returns the plane's transparent color. Otherwise returns an invalid color.

The returned color's pixel value is the index of the transparent color in the colormap of the overlay plane. (Naturally, the color's RGB values are meaningless.)

The returned QColor object will generally work as expected only when passed as the argument to QGLWidget.qglColor() or QGLWidget.qglClearColor(). Under certain circumstances it can also be used to draw transparent graphics with a QPainter. See the examples/opengl/overlay_x11 example for details.

### QGLFormat QGLContext.requestedFormat (self)

Returns the frame buffer format that was originally requested in the constructor or setFormat().

See also format().

### QGLContext.reset (self)

Resets the context and makes it invalid.

See also create() and isValid().

### QGLContext.setFormat (self, QGLFormat format)

Sets a format for this context. The context is reset.

Call create() to create a new GL context that tries to match the new format.

     QGLContext *cx;
     //  ...
     QGLFormat f;
     f.setStereo(true);
     cx->setFormat(f);
     if (!cx->create())
         exit(); // no OpenGL support, or cannot render on the specified paintdevice
     if (!cx->format().stereo())
         exit(); // could not create stereo context

See also format(), reset(), and create().

### QGLContext.setInitialized (self, bool on)

If on is true the context has been initialized, i.e. QGLContext.setInitialized() has been called on it. If on is false the context has not been initialized.

See also initialized().

### QGLContext.setTextureCacheLimit (int size)

This function sets the limit for the texture cache to size, expressed in kilobytes.

By default, the cache limit is approximately 64 MB.

See also textureCacheLimit().

### QGLContext.setWindowCreated (self, bool on)

If on is true the context has had a window created for it. If on is false no window has been created for the context.

See also windowCreated().

### QGLContext.swapBuffers (self)

Swaps the screen contents with an off-screen buffer. Only works if the context is in double buffer mode.

See also QGLFormat.setDoubleBuffer().

### int QGLContext.textureCacheLimit ()

Returns the current texture cache limit in kilobytes.

See also setTextureCacheLimit().

### bool QGLContext.windowCreated (self)

Returns true if a window has been created for this context; otherwise returns false.

See also setWindowCreated().