pyqtgraph.opengl VS OpenGL.GL - how to display axis at PyQt5 OpenGL embedded widget

纵饮孤独 提交于 2019-12-06 20:39:29

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:

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_())

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!