QGLWidget的奇怪行为

问题描述

使用以下代码在python中运行非常简单的示例时:

class VisualWindow(QMainWindow):

    def __init__(self,size=(600,600)):
        self.app = QApplication(sys.argv)

        super().__init__()

        self.widget = VisualWidget()

        self.setCentralWidget(self.widget)
        self.resize(*size)

        self.setwindowTitle('Viewer')

    def run(self):
        self.show()
        self.app.exec_()

class VisualWidget(QGLWidget):

    def __init__(self,color=(255,255)):
        self.context = QGLContext(QGLFormat())
        super().__init__(self.context)
        self.background_color = tuple(c/255 for c in color)
        self.context.makeCurrent()

    def sizeHint(self):
        return QtCore.QSize(800,600)

    def minimumSizeHint(self):
        return QtCore.QSize(600,600)

    def initializeGL(self):
        glEnable(GL_DEPTH_TEST)
        glDepthFunc(GL_LESS)

    def paintGL(self):
        glClearColor(*self.background_color)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glViewport(0,self.width(),self.height())

    def resizeGL(self,width,height):
        glViewport(0,height)

并在上方执行:

window  = VisualWindow()
window.run()

运行应用程序时,我得到窗口的随机行为。 (见图)

正常运行

Successful

什么?

Failed

我多次运行脚本,有时会不定期地得到不同的结果(正常和奇怪)。如果我尝试调整窗口大小,则一切正常,即在调用resizeGL时,它的显示与正常情况下相同。我在做什么错了?

OS: Linux Mint 19.2 Tina
OpenGL version string: 3.3 (Compatible Profile) Mesa 19.0.2
PyOpenGL version: 3.1.1a1
Python: Python 3.8.3 [GCC 7.3.0] :: Anaconda,Inc. on linux
Qt: PyQt5

更新。

根据PyQt文档,QGLWidget的构造函数可以接受以下方式之一的参数:

QGLWidget(parent: QWidget = None,shareWidget: QGLWidget = None,flags: Union[Qt.WindowFlags,Qt.WindowType] = Qt.WindowFlags())
QGLWidget(QGLContext,parent: QWidget = None,Qt.WindowType] = Qt.WindowFlags())
QGLWidget(QGLFormat,Qt.WindowType] = Qt.WindowFlags())

修改了VisualWidget的construcotr,如下所示:

def __init__(self,window,255)):
    self.context = QGLContext(QGLFormat())
    self.window = window

    super().__init__(self.context,self.window)
    self.background_color = tuple(c/255 for c in color)
    self.context.makeCurrent()

行为略有变化。错误结果现在很少出现。但是现在看起来

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)