PyQt6中的最小OpenGL示例不起作用,错误:glClearColor中的“操作无效”

balp4ylt  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(337)

我试着运行一个非常简单的OpenGL示例:

import sys

from OpenGL import GL as gl
from PyQt6.QtOpenGLWidgets import QOpenGLWidget
from PyQt6.QtWidgets import QApplication

class Widget(QOpenGLWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt6, OpenGL 3.3")
        self.resize(400, 400)

    def initializeGL(self):
        gl.glClearColor(0.5, 0.5, 0.5, 1)

    def paintGL(self):
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec())

但我有一个错误:

Traceback (most recent call last):
  File "main.py", line 16, in initializeGL
    gl.glClearColor(0, 0, 0, 1)
  File "E:ProgramFilesPythonPython38libsite-packagesOpenGLplatformbaseplatform.py", line 415, in __call__
    return self( *args,**named )
  File "E:ProgramFilesPythonPython38libsite-packagesOpenGLerror.py", line 230, in glCheckError
    raise self._errorClass(
OpenGL.error.GLError: GLError(
        err = 1282,
        description = b'invalid operation',
        baseOperation = glClearColor,
        cArguments = (0, 0, 0, 1)
)

这是相同的示例,但在PyQt5中是有效的:

import sys

from OpenGL import GL as gl
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QOpenGLWidget

class Widget(QOpenGLWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt5, OpenGL 3.3")
        self.resize(400, 400)

    def initializeGL(self):
        gl.glClearColor(0.5, 0.5, 0.5, 1)

    def paintGL(self):
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

if __name__ == "__main__":
    QApplication.setAttribute(Qt.AA_UseDesktopOpenGL)
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

有两点不同:

1.QOpenGLWidget迁移至PyQt6.QtOpenGLWidget
1.1.PyQt5示例中的行:QApplication.setAttribute(Qt.AA_UseDesktopOpenGL)

up9lanfz

up9lanfz1#

这一行:

QApplication.setAttribute(Qt.AA_UseDesktopOpenGL)

应替换为:

QApplication.setAttribute(Qt.ApplicationAttribute.AA_UseDesktopOpenGL)

这些更改也适用于PySide6:

1.OpenGL类已移至单独的PyQt6.QtOpenGL命名空间:

PyQt5:

from PyQt5.QtGui import (QOpenGLBuffer, QOpenGLShader, QOpenGLShaderProgram,
                         QOpenGLTexture)

PyQt6:

from PyQt6.QtOpenGL import (QOpenGLBuffer, QOpenGLShader, QOpenGLShaderProgram,
                            QOpenGLTexture)

1.QOpenGLWidget类已移至PyQt6.QtOpenGLWidget命名空间:

PyQt5:

from PyQt5.QtWidgets import QApplication, QOpenGLWidget

PyQt6:

from PyQt6.QtOpenGLWidgets import QOpenGLWidget

1.更改了着色器类型的枚举:

PyQt5:

self.program.addShaderFromSourceCode(QOpenGLShader.Vertex, vertShaderSrc)
self.program.addShaderFromSourceCode(QOpenGLShader.Fragment, fragShaderSrc)

PyQt6:

self.program.addShaderFromSourceCode(QOpenGLShader.ShaderTypeBit.Vertex, vertShaderSrc)
self.program.addShaderFromSourceCode(QOpenGLShader.ShaderTypeBit.Fragment, fragShaderSrc)

1.已更改枚举目标纹理:

PyQt5:

self.texture = QOpenGLTexture(QOpenGLTexture.Target2D)

PyQt6:

self.texture = QOpenGLTexture(QOpenGLTexture.Target.Target2D)

1.更改了设置纹理滤镜的枚举:

PyQt5:

self.texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear)

PyQt6:

self.texture.setMinMagFilters(QOpenGLTexture.Filter.Linear, QOpenGLTexture.Filter.Linear)

1.更改了WrapMode的枚举:

PyQt5:

self.texture.setWrapMode(QOpenGLTexture.ClampToEdge)

PyQt6:

self.texture.setWrapMode(QOpenGLTexture.WrapMode.ClampToEdge)

1.更改枚举以设置应用程序属性:

PyQt5:

QApplication.setAttribute(Qt.AA_UseDesktopOpenGL)

PyQt6:

QApplication.setAttribute(Qt.ApplicationAttribute.AA_UseDesktopOpenGL)

几个基本的非图形更改:

1.更改了文件打开模式的枚举:

PyQt5:

file = QFile(path)
if not file.open(QIODevice.ReadOnly):
    print("Failed to open the file: " + path)

PyQt6:

file = QFile(path)
if not file.open(QIODevice.OpenModeFlag.ReadOnly):
    print("Failed to open the file: " + path)

1.QApplication.exec_()方法已重命名为QApplication.exec()

PyQt5:

import sys
from PyQt5.QtWidgets import QApplication

app = QApplication(sys.argv)
sys.exit(app.exec_())

PyQt6:

import sys
from PyQt6.QtWidgets import QApplication

app = QApplication(sys.argv)
sys.exit(app.exec())

落地科拉达立方体与子弹物理,OpenGL 3.3:

您应该安装以下程序包:

  • PIP安装PyQt6(或PySide6)
  • PIP安装PyOpenGL
  • PIP安装NumPy
  • PIP安装Panda3D(用于Bullet物理)

相关问题