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

Synopsis¶
Functions¶
def
bindTexture
(fileName)def
bindTexture
(image, target, format, options)def
bindTexture
(image[, target=GL_TEXTURE_2D[, format=GL_RGBA]])def
bindTexture
(pixmap, target, format, options)def
bindTexture
(pixmap[, target=GL_TEXTURE_2D[, format=GL_RGBA]])def
colorIndex
(c)def
contextHandle
()def
deleteTexture
(tx_id)def
device
()def
deviceIsPixmap
()def
drawTexture
(point, textureId[, textureTarget=GL_TEXTURE_2D])def
drawTexture
(target, textureId[, textureTarget=GL_TEXTURE_2D])def
format
()def
initialized
()def
isSharing
()def
isValid
()def
moveToThread
(thread)def
overlayTransparentColor
()def
requestedFormat
()def
reset
()def
setDevice
(pDev)def
setFormat
(format)def
setInitialized
(on)def
setValid
(valid)def
setWindowCreated
(on)def
windowCreated
()
Virtual functions¶
def
chooseContext
([shareContext=None])def
create
([shareContext=None])def
doneCurrent
()def
makeCurrent
()def
swapBuffers
()
Static functions¶
def
areSharing
(context1, context2)def
currentContext
()def
fromOpenGLContext
(platformContext)def
setTextureCacheLimit
(size)def
textureCacheLimit
()
Detailed Description¶
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 withsetFormat()
. The format options that are actually set are returned byformat()
; the options you asked for are returned byrequestedFormat()
. Note that after aQGLContext
object has been constructed, the actual OpenGL context must be created by explicitly calling thecreate()
function. ThemakeCurrent()
function makes this context the current rendering context. You can make no context current usingdoneCurrent()
. Thereset()
function will reset the context and make it invalid.You can examine properties of the context with, e.g.
isValid()
,isSharing()
,initialized()
,windowCreated()
andoverlayTransparentColor()
.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.
-
class
QGLContext
(format)¶ - param format
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, theformat()
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 beinvalid
if it was not possible to obtain a GL context at all.
-
PySide2.QtOpenGL.QGLContext.
BindOption
¶ A set of options to decide how to bind a texture using
bindTexture()
.Constant
Description
QGLContext.NoBindOption
Don’t do anything, pass the texture straight through.
QGLContext.InvertedYBindOption
Specifies that the texture should be flipped over the X axis so that the texture coordinate 0,0 corresponds to the top left corner. Inverting the texture implies a deep copy prior to upload.
QGLContext.MipmapBindOption
Specifies that
bindTexture()
should try to generate mipmaps. If the GL implementation supports theGL_SGIS_generate_mipmap
extension, mipmaps will be automatically generated for the texture. Mipmap generation is only supported for theGL_TEXTURE_2D
target.QGLContext.PremultipliedAlphaBindOption
Specifies that the image should be uploaded with premultiplied alpha and does a conversion accordingly.
QGLContext.LinearFilteringBindOption
Specifies that the texture filtering should be set to GL_LINEAR. Default is GL_NEAREST. If mipmap is also enabled, filtering will be set to GL_LINEAR_MIPMAP_LINEAR.
QGLContext.DefaultBindOption
In Qt 4.5 and earlier,
bindTexture()
would mirror the image and automatically generate mipmaps. This option helps preserve this default behavior.
New in version 4.6.
-
static
PySide2.QtOpenGL.QGLContext.
areSharing
(context1, context2)¶ - Parameters
context1 –
QGLContext
context2 –
QGLContext
- Return type
bool
Returns
true
ifcontext1
andcontext2
are sharing their GL resources such as textures, shader programs, etc; otherwise returnsfalse
.
-
PySide2.QtOpenGL.QGLContext.
bindTexture
(image[, target=GL_TEXTURE_2D[, format=GL_RGBA]])¶ - Parameters
image –
QImage
target –
GLenum
format –
GLint
- Return type
GLuint
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 laterglBindTexture()
calls.This is an overloaded function.
-
PySide2.QtOpenGL.QGLContext.
bindTexture
(image, target, format, options) - Parameters
image –
QImage
target –
GLenum
format –
GLint
options –
BindOptions
- Return type
GLuint
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 laterglBindTexture()
calls.The
target
parameter specifies the texture target. The default target isGL_TEXTURE_2D
.The
format
parameter sets the internal format for the texture. The default format isGL_RGBA
.The binding
options
are a set of options used to decide how to bind the texture to the context.The texture that is generated is cached, so multiple calls to with the same
QImage
will return the same texture id.Note that we assume default values for the glPixelStore() and glPixelTransfer() parameters.
See also
-
PySide2.QtOpenGL.QGLContext.
bindTexture
(pixmap[, target=GL_TEXTURE_2D[, format=GL_RGBA]]) - Parameters
pixmap –
QPixmap
target –
GLenum
format –
GLint
- Return type
GLuint
This is an overloaded function.
Generates and binds a 2D GL texture based on
pixmap
.
-
PySide2.QtOpenGL.QGLContext.
bindTexture
(pixmap, target, format, options) - Parameters
pixmap –
QPixmap
target –
GLenum
format –
GLint
options –
BindOptions
- Return type
GLuint
This is an overloaded function.
Generates and binds a 2D GL texture to the current context, based on
pixmap
.
-
PySide2.QtOpenGL.QGLContext.
bindTexture
(fileName) - Parameters
fileName – unicode
- Return type
GLuint
This is an overloaded function.
Reads the compressed texture file
fileName
and generates a 2D GL texture from it.This function can load DirectDrawSurface (DDS) textures in the DXT1, DXT3 and DXT5 DDS formats if the
GL_ARB_texture_compression
andGL_EXT_texture_compression_s3tc
extensions are supported.Since 4.6.1, textures in the ETC1 format can be loaded if the
GL_OES_compressed_ETC1_RGB8_texture
extension is supported and the ETC1 texture has been encapsulated in the PVR container format. Also, textures in the PVRTC2 and PVRTC4 formats can be loaded if theGL_IMG_texture_compression_pvrtc
extension is supported.See also
-
PySide2.QtOpenGL.QGLContext.
chooseContext
([shareContext=None])¶ - Parameters
shareContext –
QGLContext
- Return type
bool
This semi-internal function is called by
create()
. It creates a system-dependent OpenGL handle that matches theformat()
ofshareContext
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.
-
PySide2.QtOpenGL.QGLContext.
colorIndex
(c)¶ - Parameters
c –
QColor
- Return type
uint
Returns a colormap index for the color c, in ColorIndex mode. Used by qglColor() and qglClearColor().
-
PySide2.QtOpenGL.QGLContext.
contextHandle
()¶ - Return type
QOpenGLContext
Returns the OpenGL context handle.
-
PySide2.QtOpenGL.QGLContext.
create
([shareContext=None])¶ - Parameters
shareContext –
QGLContext
- Return type
bool
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 returnsfalse
(i.e. the context is invalid).If the OpenGL implementation on your system does not support the requested version of OpenGL context, then
QGLContext
will try to create the closest matching version. The actual created context properties can be queried using theQGLFormat
returned by theformat()
function. For example, if you request a context that supports OpenGL 4.3 Core profile but the driver and/or hardware only supports version 3.2 Core profile contexts then you will get a 3.2 Core profile context.After successful creation,
format()
returns the set of features of the created GL rendering context.If
shareContext
points to a validQGLContext
, this method will try to establish OpenGL display list and texture object sharing between this context and theshareContext
. Note that this may fail if the two contexts have differentformats
. UseisSharing()
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 functionschooseContext()
(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 function.See also
-
static
PySide2.QtOpenGL.QGLContext.
currentContext
()¶ - Return type
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
-
PySide2.QtOpenGL.QGLContext.
deleteTexture
(tx_id)¶ - Parameters
tx_id –
GLuint
Removes the texture identified by
id
from the texture cache, and calls glDeleteTextures() to delete the texture from the context.See also
-
PySide2.QtOpenGL.QGLContext.
device
()¶ - Return type
QPaintDevice
Returns the paint device set for this context.
See also
QGLContext()
-
PySide2.QtOpenGL.QGLContext.
deviceIsPixmap
()¶ - Return type
bool
Returns
true
if the paint device of this context is a pixmap; otherwise returnsfalse
.Since Qt 5 the paint device is never actually a pixmap. renderPixmap() is however still simulated using framebuffer objects and readbacks, and this function will return
true
in this case.
-
PySide2.QtOpenGL.QGLContext.
doneCurrent
()¶ Makes no GL context the current context. Normally, you do not need to call this function;
QGLContext
calls it as necessary.
-
PySide2.QtOpenGL.QGLContext.
drawTexture
(point, textureId[, textureTarget=GL_TEXTURE_2D])¶ - Parameters
point –
QPointF
textureId –
GLuint
textureTarget –
GLenum
This function supports the following use cases:
By default it draws the given texture,
textureId
, at the givenpoint
in OpenGL model space. ThetextureTarget
should be a 2D texture target.If a painter is active, not inside a beginNativePainting / endNativePainting block, and uses the engine with type
OpenGL2
, the function will draw the given texture,textureId
, at the givenpoint
, respecting the current painter state. This will let you draw a texture with the clip, transform, render hints, and composition mode set by the painter. Note that the texture target needs to be GL_TEXTURE_2D for this use case.
Note
This function is not supported under any version of OpenGL ES.
-
PySide2.QtOpenGL.QGLContext.
drawTexture
(target, textureId[, textureTarget=GL_TEXTURE_2D]) - Parameters
target –
QRectF
textureId –
GLuint
textureTarget –
GLenum
This function supports the following use cases:
On OpenGL and OpenGL ES 1.x it draws the given texture,
textureId
, to the given target rectangle,target
, in OpenGL model space. ThetextureTarget
should be a 2D texture target.On OpenGL and OpenGL ES 2.x, if a painter is active, not inside a beginNativePainting / endNativePainting block, and uses the engine with type
OpenGL2
, the function will draw the given texture,textureId
, to the given target rectangle,target
, respecting the current painter state. This will let you draw a texture with the clip, transform, render hints, and composition mode set by the painter. Note that the texture target needs to be GL_TEXTURE_2D for this use case, and that this is the only supported use case under OpenGL ES 2.x.
-
PySide2.QtOpenGL.QGLContext.
format
()¶ - Return type
Returns the frame buffer format that was obtained (this may be a subset of what was requested).
See also
-
static
PySide2.QtOpenGL.QGLContext.
fromOpenGLContext
(platformContext)¶ - Parameters
platformContext –
QOpenGLContext
- Return type
Returns an OpenGL context for the window context specified by the
context
parameter.
-
PySide2.QtOpenGL.QGLContext.
initialized
()¶ - Return type
bool
Returns
true
if this context has been initialized, i.e. ifinitializeGL()
has been performed on it; otherwise returnsfalse
.See also
-
PySide2.QtOpenGL.QGLContext.
isSharing
()¶ - Return type
bool
Returns
true
if this context is sharing its GL context with anotherQGLContext
, otherwise false is returned. Note that context sharing might not be supported between contexts with different formats.
-
PySide2.QtOpenGL.QGLContext.
isValid
()¶ - Return type
bool
Returns
true
if a GL rendering context has been successfully created; otherwise returnsfalse
.
-
PySide2.QtOpenGL.QGLContext.
makeCurrent
()¶ 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.
If you call this from a thread other than the main UI thread, make sure you’ve first pushed the context to the relevant thread from the UI thread using
moveToThread()
.
-
PySide2.QtOpenGL.QGLContext.
moveToThread
(thread)¶ - Parameters
thread –
QThread
Moves the
QGLContext
to the giventhread
.Enables calling
swapBuffers()
andmakeCurrent()
on the context in the given thread.
-
PySide2.QtOpenGL.QGLContext.
overlayTransparentColor
()¶ - Return type
QColor
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 toqglColor()
orqglClearColor()
. Under certain circumstances it can also be used to draw transparent graphics with aQPainter
.
-
PySide2.QtOpenGL.QGLContext.
requestedFormat
()¶ - Return type
Returns the frame buffer format that was originally requested in the constructor or
setFormat()
.See also
-
PySide2.QtOpenGL.QGLContext.
reset
()¶ Resets the context and makes it invalid.
-
PySide2.QtOpenGL.QGLContext.
setFormat
(format)¶ - Parameters
format –
QGLFormat
Sets a
format
for this context. The context isreset
.Call
create()
to create a new GL context that tries to match the new format.cx = QGLContext() # ... f = QGLFormat() 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
-
PySide2.QtOpenGL.QGLContext.
setInitialized
(on)¶ - Parameters
on –
bool
If
on
is true the context has been initialized, i.e. has been called on it. Ifon
is false the context has not been initialized.See also
-
static
PySide2.QtOpenGL.QGLContext.
setTextureCacheLimit
(size)¶ - Parameters
size –
int
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
-
PySide2.QtOpenGL.QGLContext.
setValid
(valid)¶ - Parameters
valid –
bool
Forces the GL rendering context to be valid.
See also
-
PySide2.QtOpenGL.QGLContext.
setWindowCreated
(on)¶ - Parameters
on –
bool
If
on
is true the context has had a window created for it. Ifon
is false no window has been created for the context.See also
-
PySide2.QtOpenGL.QGLContext.
swapBuffers
()¶ Call this to finish a frame of OpenGL rendering, and make sure to call
makeCurrent()
again before issuing any further OpenGL commands, for example as part of a new frame.
-
static
PySide2.QtOpenGL.QGLContext.
textureCacheLimit
()¶ - Return type
int
Returns the current texture cache limit in kilobytes.
See also
-
PySide2.QtOpenGL.QGLContext.
windowCreated
()¶ - Return type
bool
Returns
true
if a window has been created for this context; otherwise returnsfalse
.See also