QGLWidget

Inheritance diagram of QGLWidget

Synopsis

Functions

Virtual functions

Static functions

Detailed Description

The PySide.QtOpenGL.QGLWidget class is a widget for rendering OpenGL graphics.

PySide.QtOpenGL.QGLWidget provides functionality for displaying OpenGL graphics integrated into a Qt application. It is very simple to use. You inherit from it and use the subclass like any other PySide.QtGui.QWidget , except that you have the choice between using PySide.QtGui.QPainter and standard OpenGL rendering commands.

PySide.QtOpenGL.QGLWidget provides three convenient virtual functions that you can reimplement in your subclass to perform the typical OpenGL tasks:

Here is a rough outline of how a PySide.QtOpenGL.QGLWidget subclass might look:

class MyGLDrawer(QGLWidget):

    def __init__(self, parent):
        QGLWidget.__init__(self, parent)
        pass

    def initializeGL(self):
        # Set up the rendering context, define display lists etc.:
        ...
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glEnable(GL_DEPTH_TEST)
        ...

    def resizeGL(self, w, h):
        # setup viewport, projection etc.:
        glViewport(0, 0, w, h)
        ...
        glFrustum(...)
        ...

    def paintGL(self):
        # draw the scene:
        ...
        glRotatef(...)
        glMaterialfv(...)
        glBegin(GL_QUADS)
        glVertex3f(...)
        glVertex3f(...)
        ...
        glEnd()
        ...

If you need to trigger a repaint from places other than PySide.QtOpenGL.QGLWidget.paintGL() (a typical example is when using timers to animate scenes), you should call the widget’s PySide.QtOpenGL.QGLWidget.updateGL() function.

Your widget’s OpenGL rendering context is made current when PySide.QtOpenGL.QGLWidget.paintGL() , PySide.QtOpenGL.QGLWidget.resizeGL() , or PySide.QtOpenGL.QGLWidget.initializeGL() is called. If you need to call the standard OpenGL API functions from other places (e.g. in your widget’s constructor or in your own paint functions), you must call PySide.QtOpenGL.QGLWidget.makeCurrent() first.

PySide.QtOpenGL.QGLWidget provides functions for requesting a new display format and you can also create widgets with customized rendering contexts .

You can also share OpenGL display lists between PySide.QtOpenGL.QGLWidget objects (see the documentation of the PySide.QtOpenGL.QGLWidget constructors for details).

Note that under Windows, the PySide.QtOpenGL.QGLContext belonging to a PySide.QtOpenGL.QGLWidget has to be recreated when the PySide.QtOpenGL.QGLWidget is reparented. This is necessary due to limitations on the Windows platform. This will most likely cause problems for users that have subclassed and installed their own PySide.QtOpenGL.QGLContext on a PySide.QtOpenGL.QGLWidget . It is possible to work around this issue by putting the PySide.QtOpenGL.QGLWidget inside a dummy widget and then reparenting the dummy widget, instead of the PySide.QtOpenGL.QGLWidget . This will side-step the issue altogether, and is what we recommend for users that need this kind of functionality.

On Mac OS X, when Qt is built with Cocoa support, a PySide.QtOpenGL.QGLWidget can’t have any sibling widgets placed ontop of itself. This is due to limitations in the Cocoa API and is not supported by Apple.

Overlays

The PySide.QtOpenGL.QGLWidget creates a GL overlay context in addition to the normal context if overlays are supported by the underlying system.

If you want to use overlays, you specify it in the format . (Note: Overlay must be requested in the format passed to the PySide.QtOpenGL.QGLWidget constructor.) Your GL widget should also implement some or all of these virtual methods:

These methods work in the same way as the normal PySide.QtOpenGL.QGLWidget.paintGL() etc. functions, except that they will be called when the overlay context is made current. You can explicitly make the overlay context current by using PySide.QtOpenGL.QGLWidget.makeOverlayCurrent() , and you can access the overlay context directly (e.g. to ask for its transparent color) by calling PySide.QtOpenGL.QGLWidget.overlayContext() .

On X servers in which the default visual is in an overlay plane, non-GL Qt windows can also be used for overlays.

Painting Techniques

As described above, subclass PySide.QtOpenGL.QGLWidget to render pure 3D content in the following way:

It is also possible to draw 2D graphics onto a PySide.QtOpenGL.QGLWidget subclass, it is necessary to reimplement QGLWidget.paintEvent() and do the following:

  • Construct a PySide.QtGui.QPainter object.
  • Initialize it for use on the widget with the QPainter.begin() function.
  • Draw primitives using PySide.QtGui.QPainter ‘s member functions.
  • Call QPainter.end() to finish painting.

Overpainting 2D content on top of 3D content takes a little more effort. One approach to doing this is shown in the Overpainting example.

Threading

It is possible to render into a PySide.QtOpenGL.QGLWidget from another thread, but it requires that all access to the GL context is safe guarded. The Qt GUI thread will try to use the context in resizeEvent and paintEvent, so in order for threaded rendering using a GL widget to work, these functions need to be intercepted in the GUI thread and handled accordingly in the application.

OpenGL is a trademark of Silicon Graphics, Inc. in the United States and other countries.

See also

PySide.QtOpenGL.QGLPixelBuffer Hello GL Example 2D Painting Example Overpainting Example Grabber Example

class PySide.QtOpenGL.QGLWidget(context[, parent=None[, shareWidget=None[, f=0]]])
class PySide.QtOpenGL.QGLWidget([parent=None[, shareWidget=None[, f=0]]])
class PySide.QtOpenGL.QGLWidget(format[, parent=None[, shareWidget=None[, f=0]]])
Parameters:
PySide.QtOpenGL.QGLWidget.autoBufferSwap()
Return type:PySide.QtCore.bool

Returns true if the widget is doing automatic GL buffer swapping; otherwise returns false.

PySide.QtOpenGL.QGLWidget.bindTexture(pixmap, target, format, options)
Parameters:
  • pixmapPySide.QtGui.QPixmap
  • target – long
  • formatPySide.QtCore.int
  • optionsPySide.QtOpenGL.QGLContext.BindOptions
Return type:

long

PySide.QtOpenGL.QGLWidget.bindTexture(pixmap[, target=0x0DE1[, format=0x1908]])
Parameters:
Return type:

long

PySide.QtOpenGL.QGLWidget.bindTexture(fileName)
Parameters:fileName – unicode
Return type:long

This is an overloaded function.

Calls QGLContext::bindTexture(fileName ) on the currently set context.

PySide.QtOpenGL.QGLWidget.bindTexture(image, target, format, options)
Parameters:
  • imagePySide.QtGui.QImage
  • target – long
  • formatPySide.QtCore.int
  • optionsPySide.QtOpenGL.QGLContext.BindOptions
Return type:

long

PySide.QtOpenGL.QGLWidget.bindTexture(image[, target=0x0DE1[, format=0x1908]])
Parameters:
Return type:

long

PySide.QtOpenGL.QGLWidget.colormap()
Return type:PySide.QtOpenGL.QGLColormap

Returns the colormap for this widget.

Usually it is only top-level widgets that can have different colormaps installed. Asking for the colormap of a child widget will return the colormap for the child’s top-level widget.

If no colormap has been set for this widget, the PySide.QtOpenGL.QGLColormap returned will be empty.

PySide.QtOpenGL.QGLWidget.context()
Return type:PySide.QtOpenGL.QGLContext

Returns the context of this widget.

It is possible that the context is not valid (see PySide.QtOpenGL.QGLWidget.isValid() ), for example, if the underlying hardware does not support the format attributes that were requested.

static PySide.QtOpenGL.QGLWidget.convertToGLFormat(img)
Parameters:imgPySide.QtGui.QImage
Return type:PySide.QtGui.QImage

Converts the image img into the unnamed format expected by OpenGL functions such as glTexImage2D(). The returned image is not usable as a PySide.QtGui.QImage , but QImage.width() , QImage.height() and QImage.bits() may be used with OpenGL. The GL format used is GL_RGBA .

PySide.QtOpenGL.QGLWidget.deleteTexture(tx_id)
Parameters:tx_id – long
PySide.QtOpenGL.QGLWidget.doneCurrent()

Makes no GL context the current context. Normally, you do not need to call this function; PySide.QtOpenGL.QGLContext calls it as necessary. However, it may be useful in multithreaded environments.

PySide.QtOpenGL.QGLWidget.doubleBuffer()
Return type:PySide.QtCore.bool

Returns true if the contained GL rendering context has double buffering; otherwise returns false.

PySide.QtOpenGL.QGLWidget.drawTexture(target, textureId[, textureTarget=0x0DE1])
Parameters:
PySide.QtOpenGL.QGLWidget.drawTexture(point, textureId[, textureTarget=0x0DE1])
Parameters:
PySide.QtOpenGL.QGLWidget.format()
Return type:PySide.QtOpenGL.QGLFormat

Returns the format of the contained GL rendering context.

See also

PySide.QtOpenGL.QGLWidget.setFormat()

PySide.QtOpenGL.QGLWidget.glDraw()

Executes the virtual function PySide.QtOpenGL.QGLWidget.paintGL() .

The widget’s rendering context will become the current context and PySide.QtOpenGL.QGLWidget.initializeGL() will be called if it hasn’t already been called.

PySide.QtOpenGL.QGLWidget.glInit()

Initializes OpenGL for this widget’s context. Calls the virtual function PySide.QtOpenGL.QGLWidget.initializeGL() .

PySide.QtOpenGL.QGLWidget.grabFrameBuffer([withAlpha=false])
Parameters:withAlphaPySide.QtCore.bool
Return type:PySide.QtGui.QImage

Returns an image of the frame buffer. If withAlpha is true the alpha channel is included.

Depending on your hardware, you can explicitly select which color buffer to grab with a glReadBuffer() call before calling this function.

PySide.QtOpenGL.QGLWidget.initializeGL()

This virtual function is called once before the first call to PySide.QtOpenGL.QGLWidget.paintGL() or PySide.QtOpenGL.QGLWidget.resizeGL() , and then once whenever the widget has been assigned a new PySide.QtOpenGL.QGLContext . Reimplement it in a subclass.

This function should set up any required OpenGL context rendering flags, defining display lists, etc.

There is no need to call PySide.QtOpenGL.QGLWidget.makeCurrent() because this has already been done when this function is called.

PySide.QtOpenGL.QGLWidget.initializeOverlayGL()

This virtual function is used in the same manner as PySide.QtOpenGL.QGLWidget.initializeGL() except that it operates on the widget’s overlay context instead of the widget’s main context. This means that PySide.QtOpenGL.QGLWidget.initializeOverlayGL() is called once before the first call to PySide.QtOpenGL.QGLWidget.paintOverlayGL() or PySide.QtOpenGL.QGLWidget.resizeOverlayGL() . Reimplement it in a subclass.

This function should set up any required OpenGL context rendering flags, defining display lists, etc. for the overlay context.

There is no need to call PySide.QtOpenGL.QGLWidget.makeOverlayCurrent() because this has already been done when this function is called.

PySide.QtOpenGL.QGLWidget.isSharing()
Return type:PySide.QtCore.bool

Returns true if this widget’s GL context is shared with another GL context, otherwise false is returned. Context sharing might not be possible if the widgets use different formats.

PySide.QtOpenGL.QGLWidget.isValid()
Return type:PySide.QtCore.bool

Returns true if the widget has a valid GL rendering context; otherwise returns false. A widget will be invalid if the system has no OpenGL support .

PySide.QtOpenGL.QGLWidget.makeCurrent()

Makes this widget the current widget for OpenGL operations, i.e. makes the widget’s rendering context the current OpenGL rendering context.

PySide.QtOpenGL.QGLWidget.makeOverlayCurrent()

Makes the overlay context of this widget current. Use this if you need to issue OpenGL commands to the overlay context outside of PySide.QtOpenGL.QGLWidget.initializeOverlayGL() , PySide.QtOpenGL.QGLWidget.resizeOverlayGL() , and PySide.QtOpenGL.QGLWidget.paintOverlayGL() .

Does nothing if this widget has no overlay.

PySide.QtOpenGL.QGLWidget.overlayContext()
Return type:PySide.QtOpenGL.QGLContext

Returns the overlay context of this widget, or 0 if this widget has no overlay.

PySide.QtOpenGL.QGLWidget.paintGL()

This virtual function is called whenever the widget needs to be painted. Reimplement it in a subclass.

There is no need to call PySide.QtOpenGL.QGLWidget.makeCurrent() because this has already been done when this function is called.

PySide.QtOpenGL.QGLWidget.paintOverlayGL()

This virtual function is used in the same manner as PySide.QtOpenGL.QGLWidget.paintGL() except that it operates on the widget’s overlay context instead of the widget’s main context. This means that PySide.QtOpenGL.QGLWidget.paintOverlayGL() is called whenever the widget’s overlay needs to be painted. Reimplement it in a subclass.

There is no need to call PySide.QtOpenGL.QGLWidget.makeOverlayCurrent() because this has already been done when this function is called.

PySide.QtOpenGL.QGLWidget.qglClearColor(c)
Parameters:cPySide.QtGui.QColor

Convenience function for specifying the clearing color to OpenGL. Calls glClearColor (in RGBA mode) or glClearIndex (in color-index mode) with the color c . Applies to this widgets GL context.

PySide.QtOpenGL.QGLWidget.qglColor(c)
Parameters:cPySide.QtGui.QColor

Convenience function for specifying a drawing color to OpenGL. Calls glColor4 (in RGBA mode) or glIndex (in color-index mode) with the color c . Applies to this widgets GL context.

Note

This function is not supported on OpenGL/ES 2.0 systems.

PySide.QtOpenGL.QGLWidget.renderPixmap([w=0[, h=0[, useContext=false]]])
Parameters:
  • wPySide.QtCore.int
  • hPySide.QtCore.int
  • useContextPySide.QtCore.bool
Return type:

PySide.QtGui.QPixmap

Renders the current scene on a pixmap and returns the pixmap.

You can use this method on both visible and invisible PySide.QtOpenGL.QGLWidget objects.

This method will create a pixmap and a temporary PySide.QtOpenGL.QGLContext to render on the pixmap. It will then call PySide.QtOpenGL.QGLWidget.initializeGL() , PySide.QtOpenGL.QGLWidget.resizeGL() , and PySide.QtOpenGL.QGLWidget.paintGL() on this context. Finally, the widget’s original GL context is restored.

The size of the pixmap will be w pixels wide and h pixels high unless one of these parameters is 0 (the default), in which case the pixmap will have the same size as the widget.

If useContext is true, this method will try to be more efficient by using the existing GL context to render the pixmap. The default is false. Only use true if you understand the risks. Note that under Windows a temporary context has to be created and usage of the useContext parameter is not supported.

Overlays are not rendered onto the pixmap.

If the GL rendering context and the desktop have different bit depths, the result will most likely look surprising.

Note that the creation of display lists, modifications of the view frustum etc. should be done from within PySide.QtOpenGL.QGLWidget.initializeGL() . If this is not done, the temporary PySide.QtOpenGL.QGLContext will not be initialized properly, and the rendered pixmap may be incomplete/corrupted.

PySide.QtOpenGL.QGLWidget.renderText(x, y, str[, fnt=QFont()[, listBase=2000]])
Parameters:
  • xPySide.QtCore.int
  • yPySide.QtCore.int
  • str – unicode
  • fntPySide.QtGui.QFont
  • listBasePySide.QtCore.int

Renders the string str into the GL context of this widget.

x and y are specified in window coordinates, with the origin in the upper left-hand corner of the window. If font is not specified, the currently set application font will be used to render the string. To change the color of the rendered text you can use the glColor() call (or the PySide.QtOpenGL.QGLWidget.qglColor() convenience function), just before the PySide.QtOpenGL.QGLWidget.renderText() call.

The listBase parameter is obsolete and will be removed in a future version of Qt.

Note

This function clears the stencil buffer.

Note

This function is not supported on OpenGL/ES systems.

Note

This function temporarily disables depth-testing when the text is drawn.

Note

This function can only be used inside a QPainter.beginNativePainting() / QPainter.endNativePainting() block if the default OpenGL paint engine is QPaintEngine.OpenGL . To make QPaintEngine.OpenGL the default GL engine, call QGL::setPreferredPaintEngine( QPaintEngine.OpenGL ) before the PySide.QtGui.QApplication constructor.

Overpaint with QPainter.drawText() instead.

PySide.QtOpenGL.QGLWidget.renderText(x, y, z, str[, fnt=QFont()[, listBase=2000]])
Parameters:
  • xPySide.QtCore.double
  • yPySide.QtCore.double
  • zPySide.QtCore.double
  • str – unicode
  • fntPySide.QtGui.QFont
  • listBasePySide.QtCore.int

This is an overloaded function.

x , y and z are specified in scene or object coordinates relative to the currently set projection and model matrices. This can be useful if you want to annotate models with text labels and have the labels move with the model as it is rotated etc.

Note

This function is not supported on OpenGL/ES systems.

Note

If depth testing is enabled before this function is called, then the drawn text will be depth-tested against the models that have already been drawn in the scene. Use glDisable(GL_DEPTH_TEST) before calling this function to annotate the models without depth-testing the text.

Overpaint with QPainter.drawText() instead.

PySide.QtOpenGL.QGLWidget.resizeGL(w, h)
Parameters:
  • wPySide.QtCore.int
  • hPySide.QtCore.int

This virtual function is called whenever the widget has been resized. The new size is passed in width and height . Reimplement it in a subclass.

There is no need to call PySide.QtOpenGL.QGLWidget.makeCurrent() because this has already been done when this function is called.

PySide.QtOpenGL.QGLWidget.resizeOverlayGL(w, h)
Parameters:
  • wPySide.QtCore.int
  • hPySide.QtCore.int

This virtual function is used in the same manner as PySide.QtOpenGL.QGLWidget.paintGL() except that it operates on the widget’s overlay context instead of the widget’s main context. This means that PySide.QtOpenGL.QGLWidget.resizeOverlayGL() is called whenever the widget has been resized. The new size is passed in width and height . Reimplement it in a subclass.

There is no need to call PySide.QtOpenGL.QGLWidget.makeOverlayCurrent() because this has already been done when this function is called.

PySide.QtOpenGL.QGLWidget.setAutoBufferSwap(on)
Parameters:onPySide.QtCore.bool

If on is true automatic GL buffer swapping is switched on; otherwise it is switched off.

If on is true and the widget is using a double-buffered format, the background and foreground GL buffers will automatically be swapped after each PySide.QtOpenGL.QGLWidget.paintGL() call.

The buffer auto-swapping is on by default.

PySide.QtOpenGL.QGLWidget.setColormap(map)
Parameters:mapPySide.QtOpenGL.QGLColormap

Set the colormap for this widget to cmap . Usually it is only top-level widgets that can have colormaps installed.

PySide.QtOpenGL.QGLWidget.swapBuffers()

Swaps the screen contents with an off-screen buffer. This only works if the widget’s format specifies double buffer mode.

Normally, there is no need to explicitly call this function because it is done automatically after each widget repaint, i.e. each time after PySide.QtOpenGL.QGLWidget.paintGL() has been executed.

PySide.QtOpenGL.QGLWidget.updateGL()

Updates the widget by calling PySide.QtOpenGL.QGLWidget.glDraw() .

PySide.QtOpenGL.QGLWidget.updateOverlayGL()

Updates the widget’s overlay (if any). Will cause the virtual function PySide.QtOpenGL.QGLWidget.paintOverlayGL() to be executed.

The widget’s rendering context will become the current context and PySide.QtOpenGL.QGLWidget.initializeGL() will be called if it hasn’t already been called.