From 1a29cf7579ab57869055815c0c9fcd62790cb906 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 29 Jun 2021 12:27:04 +0800 Subject: [PATCH] defer init if we do not have an OpenGL context yet --- pyqtgraph/opengl/GLGraphicsItem.py | 15 ++++++++++----- pyqtgraph/opengl/GLViewWidget.py | 10 +++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pyqtgraph/opengl/GLGraphicsItem.py b/pyqtgraph/opengl/GLGraphicsItem.py index a2c2708a..bf9de81a 100644 --- a/pyqtgraph/opengl/GLGraphicsItem.py +++ b/pyqtgraph/opengl/GLGraphicsItem.py @@ -1,8 +1,7 @@ from OpenGL.GL import * from OpenGL import GL -from ..Qt import QtGui, QtCore +from ..Qt import QtCore from .. import Transform3D -from ..python2_3 import basestring GLOptions = { @@ -42,6 +41,7 @@ class GLGraphicsItem(QtCore.QObject): self.__children = set() self.__transform = Transform3D() self.__visible = True + self.__initialized = False self.setParentItem(parentItem) self.setDepthValue(0) self.__glOpts = {} @@ -91,7 +91,7 @@ class GLGraphicsItem(QtCore.QObject): """ - if isinstance(opts, basestring): + if isinstance(opts, str): opts = GLOptions[opts] self.__glOpts = opts.copy() self.update() @@ -228,6 +228,11 @@ class GLGraphicsItem(QtCore.QObject): view, as it may be obscured or outside of the current view area.""" return self.__visible + def setInitialized(self, init=True): + self.__initialized = init + + def initialized(self): + return self.__initialized def initializeGL(self): """ @@ -235,7 +240,7 @@ class GLGraphicsItem(QtCore.QObject): The widget's GL context is made current before this method is called. (So this would be an appropriate time to generate lists, upload textures, etc.) """ - pass + self.setInitialized() def setupGLState(self): """ @@ -245,7 +250,7 @@ class GLGraphicsItem(QtCore.QObject): for k,v in self.__glOpts.items(): if v is None: continue - if isinstance(k, basestring): + if isinstance(k, str): func = getattr(GL, k) func(*v) else: diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 8896bdc0..61a7d445 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -8,7 +8,6 @@ import warnings from math import cos, sin, tan, radians ##Vector = QtGui.QVector3D -ShareWidget = None class GLViewWidget(QtWidgets.QOpenGLWidget): @@ -99,10 +98,12 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): def addItem(self, item): self.items.append(item) - if hasattr(item, 'initializeGL'): + + if self.isValid(): self.makeCurrent() try: item.initializeGL() + item.setInitialized() except: self.checkOpenGLVersion('Error while adding item %s to GLViewWidget.' % str(item)) @@ -128,7 +129,10 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): self.update() def initializeGL(self): - self.resizeGL(self.width(), self.height()) + for item in self.items: + if not item.initialized(): + item.initializeGL() + item.setInitialized() def setBackgroundColor(self, *args, **kwds): """