Python QGLWidget的奇怪行为
使用以下代码在python中运行非常简单的示例时:Python QGLWidget的奇怪行为,python,opengl,pyqt5,pyopengl,Python,Opengl,Pyqt5,Pyopengl,使用以下代码在python中运行非常简单的示例时: class VisualWindow(QMainWindow): def __init__(self, size=(600, 600)): self.app = QApplication(sys.argv) super().__init__() self.widget = VisualWidget() self.setCentralWidget(self.widge
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, 0, 0, 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, 0, self.width(), self.height())
def resizeGL(self, width, height):
glViewport(0, 0, width, height)
并按以下方式执行上述内容:
window = VisualWindow()
window.run()
在运行应用程序时,我得到了窗口的随机行为。(见图)
正常运行
什么???
我运行脚本多次,并不时得到不同的结果(正常和奇怪),没有任何规律性。如果我试图调整窗口大小,一切都会正常,即在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
upd.
根据PyQt文档,QGLWidget的构造函数可以接受以下模式之一的参数:
QGLWidget(parent: QWidget = None, shareWidget: QGLWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())
QGLWidget(QGLContext, parent: QWidget = None, shareWidget: QGLWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())
QGLWidget(QGLFormat, parent: QWidget = None, shareWidget: QGLWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())
我对VisualWidget的constructor进行了如下修改:
def __init__(self, window, color=(255, 0, 0, 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()
行为略有改变。错误的结果现在很少出现。但现在看来