3d Plot not working when importing from pyqt5 but works when importing from pyqtgraph.Qt

47 views
Skip to first unread message

Daniele Dolci

unread,
Apr 18, 2020, 12:57:19 PM4/18/20
to pyqtgraph
I am trying to create a 3d plot of a cube and add it to my ui. after lots of tests and following the examples I got this code to work:

# import PyQt5
# from PyQt5 import QtGui, QtCore
# import numpy as np
# import pyqtgraph as pg
# import pyqtgraph.opengl as gl

# -*- coding: utf-8 -*-
"""
Demonstrates GLVolumeItem for displaying volumetric data.

"""

## Add path to library (just for examples; you do not need this)

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl

app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.opts['distance'] = 200
w.show()
w.setWindowTitle('pyqtgraph example: GLVolumeItem')

# b = gl.GLBoxItem()
# w.addItem(b)
g = gl.GLGridItem()
g.scale(10, 10, 1)
w.addItem(g)

import numpy as np

vertexes = np.array([[1, 0, 0], #0
[0, 0, 0], #1
[0, 1, 0], #2
[0, 0, 1], #3
[1, 1, 0], #4
[1, 1, 1], #5
[0, 1, 1], #6
[1, 0, 1]])#7

colors = np.array([[1,0,0,1] for i in range(12)])

faces = np.array([[1,0,7], [1,3,7],
[1,2,4], [1,0,4],
[1,2,6], [1,3,6],
[0,4,5], [0,7,5],
[2,4,5], [2,6,5],
[3,6,5], [3,7,5]])

cube = gl.GLMeshItem(vertexes=vertexes, faces=faces, faceColors=colors,
drawEdges=True, edgeColor=(0, 0, 0, 1))

w.addItem(cube)

ax = gl.GLAxisItem()
w.addItem(ax)

## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys

if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()


For some reason whenever I replace:

from pyqtgraph.Qt import QtCore, QtGui
with
from PyQt5 import QtWidgets, QtCore, QtGui

I start to get this error:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pyqtgraph/opengl/GLViewWidget.py", line 188, in paintGL
self.setProjection(region=region)
File "/usr/local/lib/python3.7/dist-packages/pyqtgraph/opengl/GLViewWidget.py", line 110, in setProjection
glMatrixMode(GL_PROJECTION)
File "/usr/lib/python3/dist-packages/OpenGL/error.py", line 232, in glCheckError
baseOperation = baseOperation,
OpenGL.error.GLError: GLError(
err = 1282,
description = b'invalid operation',
baseOperation = glMatrixMode,
cArguments = (GL_PROJECTION,)
)

For this reason I wasn't able to get this to work in my ui. Any idea what that might depend on?

Thanks!


Nick PV

unread,
Apr 20, 2020, 7:03:56 AM4/20/20
to pyqtgraph

Daniele Dolci

unread,
Apr 20, 2020, 7:15:30 AM4/20/20
to pyqtgraph
Hi! Thanks for the reply!

I don't use qt designer actually, but I guess i can simply translate this part of the xml into code:

<customwidgets>
   
<customwidget>
       
<class>GLViewWidget</class>
        <extends>QOpenGLWidget</extends>
        <header>pyqtgraph.opengl</header>
   
</customwidget>
</customwidgets>


Do you think something like this would work?

class GLViewWidget(QOpenGLWidget):
   
def __init__(*args, **kwargs):
       
super(GLViewWidget, self).__init__(*args, **kwargs)

Maybe I am not translating <header>pyqtgraph.opengl</header> , right?

Thanks for the help!

Daniele Dolci

unread,
Apr 20, 2020, 3:15:16 PM4/20/20
to pyqtgraph
Hi,

I did some more tests, however the custom widget doesn't seem to be the problem. I realized you meant to just let qt designer to know about the custom class and not write my own. So I am still stuck at the same point. 

Cheers,
Daniele 
Reply all
Reply to author
Forward
0 new messages