Implementing an OpenGL view into a GraphicsLayoutWidget

849 views
Skip to first unread message

Kevin Dunphy

unread,
Apr 4, 2021, 9:31:19 AM4/4/21
to pyqtgraph
Hello everyone,

I'm fairly new to PyQtGraph, and for the most part I have been having tremendous success. I am creating an application that does some analysis on objects in 3D space, and generates plots based on their geometry. Currently my plots are all 2D, with an image view and linked axes to show data arising from the underlying model geometry, however I was hoping to also include a 3D plot *showing* the geometry. Going through the PyQtGraph examples, the GlScatterPlotItem looks to be precisely what I am looking for.

Oddly enough, the example runs perfectly fine, however when I try to implement this into my application I got an error. My first instinct was to bring the GlViewWidget outside of my GraphicsLayoutWidget, and plot independently. This worked.. kind of. I can add a GridItem to the GlViewWidget, and that renders fine - I can interact with it as I can in the examples. However, upon trying to add a GlScatterPlotItem I get the following error:

line 114, in addItem
    self.checkOpenGLVersion('Error while adding item %s to GLViewWidget.' % str(item))

line 535, in checkOpenGLVersion
    verString = glGetString(GL_VERSION)

line 415, in __call__
    return self( *args, **named )

line 230, in glCheckError
    raise self._errorClass(
OpenGL.error.GLError: GLError(
err = 1282,
description = b'invalid operation',
baseOperation = glGetString,
cArguments = (GL_VERSION,)
)

What baffles me is that the examples work without issue. Some googling lead me to upgrading mesa drivers, however that did not seem to fix the issue - Any ideas on what I could do to make this work?

Nit

unread,
Apr 4, 2021, 11:06:47 PM4/4/21
to pyqtgraph
Hi, you might need to place the place the graphicsview inside gridlayout, add GLviewwidget onto the layout 

#Create object form GLviewWidget
self.viewt = gl.GLViewWidget()
#create grid features
self.xgrid.rotate(90, 0, 1, 0)
self.xgrid.translate(-10, 0, 0)
#add grid item to GLviewWidget object
self.viewt.addItem(self.xgrid)

#This should solve the issue
self.layout_parent = QGridLayout(self.graphicsView)
self.layout_parent.setContentsMargins(20, 20, 20, 20)
self.layout_parent.addWidget(self.viewt)

Let me know

Kevin Dunphy

unread,
Apr 5, 2021, 8:11:11 AM4/5/21
to pyqtgraph

This isn't quite my problem. I've attached some test scripts that show where the error occurs. The only major difference in that in one case I am embedding the GL widget into a main window, and in another case I am not. I don't think it is a driver issue *because* test2.py works without issues.

For context, I am running the following versions:

Python 3.8
PyQt5 5.15.1
pyqtgraph 0.12.0
numpy 1.19.4
test.py
test.ui
test2.py

Kevin Dunphy

unread,
Apr 5, 2021, 8:13:36 AM4/5/21
to pyqtgraph
Attached correct version of test.py **** (Still doesn't work, but uses numpy arrays in the scatter plot inputs).
test.py

Mikhail Terekhov

unread,
Apr 5, 2021, 4:29:53 PM4/5/21
to pyqt...@googlegroups.com
Probably because in your test.py the MainWindow object is garbage collected after main() returns?
After fixing it as shown below, it works for me.


from PyQt5 import QtWidgets, uic
import pyqtgraph.opengl as gl
import pyqtgraph as pg
import numpy as np


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        uic.loadUi('test.ui', self)
        self.openGLWidget.show()


def main():
    m = MainWindow()
    grid = gl.GLGridItem()
    m.openGLWidget.addItem(grid)  # <--  Works, no problem.
    pos = np.array([(0, 0, 0), (0, 1, 0)])
    size = np.array([10, 10])
    scatter_plot = gl.GLScatterPlotItem(pos=pos, size=size)
    m.openGLWidget.addItem(scatter_plot)  # <-- Throws error
    return m


if __name__ == '__main__':
    app = pg.mkQApp()
    m = main()
    m.show()
    pg.mkQApp().exec_()

Kevin Dunphy

unread,
Apr 5, 2021, 8:51:51 PM4/5/21
to pyqtgraph
I created a new environment from scratch and re-installed all my required packages with the following versions:

Python 3.8

pyqt~=5.9.2
pyqtgraph~=0.11.0
numpy~=1.19.2
scipy~=1.6.2
pandas~=1.2.3

After this, everything is working.

Ognyan Moore

unread,
Apr 5, 2021, 9:10:04 PM4/5/21
to pyqt...@googlegroups.com
FYI pyqtgraph 0.12.0+ require Qt 5.12+

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/59081bd0-1015-40f6-9772-23611e7aa0b8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages