python-3.x - openGL 和 PyQt5 的问题

标签 python-3.x opengl pyqt pyqt5 pyopengl

我正在从 Winpython3.4.3.7Qt4 迁移到 Winpython3.x.x.xQt5(我尝试了很多版本),我遇到了以下问题:

以下最小代码(它没有任何用处,但演示了错误):

from PyQt5 import QtWidgets
import OpenGL.GL as gl
from PyQt5.QtOpenGL import QGLWidget
qapp = QtWidgets.QApplication([])
window = QGLWidget()
window.makeCurrent()
index = gl.glGenLists(1)
print(index)

在我的所有机器上运行 Winpython3.4.3.7Qt4 并打印“1”。当我使用 Winpython3.x.x.xQt5 时,它不再在我的虚拟机上运行。我得到的错误是:

Traceback (most recent call last):
  File ".\opengl.py", line 12, in <module>
    index = gl.glGenLists(1)
  File "C:\Winpython-64bit-3.6.7.0\python-3.6.7\lib\site-packages\OpenGL\platform\baseplatform.py", line 405, in __call__
    return self( *args, **named )
  File "C:\Winpython-64bit-3.6.7.0\python-3.6.7\lib\site-packages\OpenGL\error.py", line 232, in glCheckError
    baseOperation = baseOperation,
OpenGL.error.GLError: GLError(
        err = 1282,
        description = b'invalid operation',
        baseOperation = glGenLists,
        cArguments = (1,),
        result = 0
)

我感觉 window.makeCurrent() 没有通过,但我不知道为什么。从 Qt4 到 Qt5 在这方面发生了什么变化?

最佳答案

根据 OpenGL 文档,glGenLists 在以下情况下将返回 GL_INVALID_OPERATION:

GL_INVALID_OPERATION is generated if glGenLists is executed between the execution of glBegin and the corresponding execution of glEnd.

看来您是在初始化 OpenGL 之前或在 glBegin glEnd 绘图调用之间调用 glGenLists

我能够通过创建一个从 QGLWidget 继承的小部件并等待它在调用 gl.glGenLists(1) 之前初始化来解决这个问题,就像你一样可以看到下面在 init 方法中发送了一个信号:

import sys
import OpenGL.GL as gl

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import pyqtSignal

from PyQt5.QtOpenGL import QGLWidget


class MyQGLWidget(QGLWidget):
    init = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)

    def glInit(self):
        super().glInit()
        self.init.emit()

    def gl_gen_lists(self, size):
        return gl.glGenLists(size)


class App(QApplication):
    def __init__(self, sys_argv):
        super().__init__(sys_argv)
        self.qgl_widget = MyQGLWidget()
        self.qgl_widget.init.connect(self.on_init)
        self.qgl_widget.show()

    def on_init(self):
        self.qgl_widget.makeCurrent()
        print(self.qgl_widget.gl_gen_lists(1))


if __name__ == '__main__':
    app = App(sys.argv)
    sys.exit(app.exec_())

错误消失了...

引用:http://docs.gl/gl3/glGenLists

关于python-3.x - openGL 和 PyQt5 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53170415/

相关文章:

python - 导入错误 : No module named '_version' when importing mechanize

python - NumPy 通过 TypeError 进行 join_functions

Python win32com 获取电子邮件的大小

c++ - 与 glDrawArrays 一起出现的 OpenGL 错误 1280

c++ - OpenGL 的范围问题

opengl - 如何使用 OpenGL 和 GLSL 手动填充深度缓冲区

python - 如何绘制跟随鼠标光标的十字?

python - gTTS 错误 : saving as wav but saved as MPEG

python - 从控制台(Ctrl-C)终止时让我的 PyQt 应用程序退出的正确方法是什么?

python - QThread 不使用事件更新 View