2

How to interact with PyQt5.QtWidgets openGLWidget using pyqtgraph.opengl instead of OpenGL.GL? I need to make a next graphical output to openGLWidget on the PyQt5 form:

def plot_line(line):
    pl_line = np.array(line)
    color = (0.0, 0.0, 200.0, 0.5)
    newline = gl.GLLinePlotItem(pos=pl_line, color=color, width=5, antialias=False)
    w.addItem(newline)
    w.show()

line1 = [(-33.13, 1004.82, -125.7), (21.38, 1059.32, -162.03)]
plot_line(line1)

Here I have an example where I have a UI with a button and openGLWidget and I want to make a graphical output to openGLWidget which I defining at plot_line() function. How should I perform such output?

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
import pyqtgraph.opengl as gl

class app_1(QDialog):
    def __init__(self):
        super(app_1,self).__init__()
        loadUi('Qt_test_Ui.ui', self)
        self.setWindowTitle('Test GL app')
        self.pushButton.clicked.connect(self.on_push_b1)

    @pyqtSlot()
    def on_push_b1(self):
        self.openGLWidget.paintGL = self.paintGL()


    def paintGL(self):
        w = self.openGLWidget
        axis = gl.GLAxisItem()  # show 3D axis 
        w.addItem(axis)


app=QApplication(sys.argv)
wid=app_1()
wid.show()
sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Nick PV
  • 415
  • 6
  • 14

1 Answers1

4

QOpenGLWidget is not a GLViewWidget so you can not replace it directly. The nearest option is that you use GLViewWidget in Qt Designer using the promotion, for it right click on QOpenGLWidget and select the option "Promote to ...", the following dialog will be opened and set with the following values:

enter image description here

Then press the add and Promote buttons. Doing the above the generated .ui is the following:

Qt_test_Ui.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="GLViewWidget" name="openGLWidget"/>
   </item>
   <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>GLViewWidget</class>
   <extends>QOpenGLWidget</extends>
   <header>pyqtgraph.opengl</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Then it is used in the following way:

import sys
from PyQt5 import QtCore, QtWidgets, uic
import pyqtgraph.opengl as gl

class app_1(QtWidgets.QDialog):
    def __init__(self):
        super(app_1,self).__init__()
        uic.loadUi('Qt_test_Ui.ui', self)
        self.setWindowTitle('Test GL app')
        self.pushButton.clicked.connect(self.on_push_b1)

    @QtCore.pyqtSlot()
    def on_push_b1(self):
        axis = gl.GLAxisItem()
        self.openGLWidget.addItem(axis)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    wid=app_1()
    wid.show()
    sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Perfect!!!! Can I ask you related to this question "How to clear up `openGLWidget` from geometry"? `self.openGLWidget.removeItem()` or `self.openGLWidget.clear()` doesn't work – Nick PV Dec 17 '18 at 22:14
  • @NickPV What do you mean by eliminating geometry? – eyllanesc Dec 17 '18 at 22:16
  • clear scene inside `openGLWidget` widget, clear including axis, just make it empty. – Nick PV Dec 17 '18 at 23:17
  • 1
    @NickPV Use `self.openGLWidget.removeItem(axis)` to remove a single item. If you want to remove all you must use: `for it in reversed(self.openGLWidget.items): self.openGLWidget.removeItem(it) del it` – eyllanesc Dec 17 '18 at 23:22