Search code examples
pythonopenglpyside2pyopengl

QOpenGLFunctions and PySide2


How to use QOpenGLFunctions from PySide2?

The problem is with the GLEnum, which the API documentation mentions but doesn't tell from where one could take it.

For example, I would like to call glGetString(), I try:

from OpenGL import GL
from PySide2.QtGui import QOpenGLFunctions as GLF

GLF.glGetString(GL.GL_VERSION))

But it produces an error.

TypeError: descriptor 'glGetString' requires a 'PySide2.QtGui.QOpenGLFunctions' object but received a 'IntConstant'

Solution

  • You have to use a QOpenGLFunctions associated with a current QOpenGLContext, for example you can use the following code:

    from OpenGL import GL
    from PySide2 import QtGui
    
    
    if __name__ == "__main__":
        app = QtGui.QGuiApplication()
        off_screen = QtGui.QOffscreenSurface()
        off_screen.create()
        if off_screen.isValid():
            context = QtGui.QOpenGLContext()
            if context.create():
                context.makeCurrent(off_screen)
                f = QtGui.QOpenGLFunctions(context)
                print(f.glGetString(GL.GL_VERSION))
    

    Output:

    3.0 Mesa 20.1.6