QOpenGLFunctions和PySide2

问题描述

如何使用QOpenGLFunctions中的pyside2

问题出在GLEnum上,api文档提到了该问题,但没有告诉人们可以从哪里拿走它。

例如,我想打电话给glGetString(),,我会尝试:

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

GLF.glGetString(GL.GL_VERSION))

但是会产生错误

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

解决方法

您必须使用与当前QOpenGLContext关联的QOpenGLFunctions,例如,可以使用以下代码:

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))

输出:

3.0 Mesa 20.1.6