使用pyside2将pyqtgraph图添加到QTDesigner

时间:2019-11-27 14:34:13

标签: python qt-designer pyside2 pyqtgraph

我有一个带有QVBoxLayout的QTDesigner文件,尝试在其中添加GLViewWidget:

我使用此Embed Pyqtgraph to PySide2示例创建了我的代码。 在我的应用程序中,所有这些都嵌入在ROS环境中。 QTDesigner QVBoxLayout:

DB::raw("CONCAT('name','id') AS ID"))

我尝试通过addWidget函数将GLViewWidget添加到QVBoxLayout:

...
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <layout class="QVBoxLayout" name="verticalLayout_2">
         <property name="leftMargin">
          <number>10</number>
         </property>
         <property name="topMargin">
          <number>10</number>
         </property>
         <property name="rightMargin">
          <number>10</number>
         </property>
         <property name="bottomMargin">
          <number>10</number>
         </property>
         <item>
          <spacer name="verticalSpacer">
           <property name="orientation">
            <enum>Qt::Vertical</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>10</width>
             <height>10</height>
...
#!/usr/bin/env python2

# ROS imports
import rospkg
import rospy

# QT imports
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile, QObject
import pyqtgraph.opengl as pg
from pyqtgraph.Qt import QtCore
import ros_numpy

class ConfigUi(QObject):

    def __init__(self, subscriber, parent=None):
        super(ConfigUi, self).__init__(parent)
        r = rospkg.RosPack()

        pkg_path = r.get_path('config_ui')
        ui_file = QFile(pkg_path + '/ui/main_window.ui')
        ui_file.open(QFile.ReadOnly)
        loader = QUiLoader()

        self.subscriber = subscriber

        self.window = loader.load(ui_file)
        self.gv = self.window.verticalLayout_2
        self.w = self.plotWidget()
        self.gv.addWidget(self.w)
        self.w.show()

    def plotWidget(self):
        w = pg.GLViewWidget()
        w.opts['distance'] = 20
        w.setWindowTitle('pyqtgraph example: GLScatterPlotItem')

        self.sp3 = pg.GLScatterPlotItem(pos=ros_numpy.point_cloud2.pointcloud2_to_xyz_array(self.subscriber.pc),
                                        color=(1, 1, 1, .3), size=0.01, pxMode=False)
        w.addItem(self.sp3)
        t = QtCore.QTimer()
        t.timeout.connect(self.update)
        t.start(50)
        return w

    def update(self):
        pc = ros_numpy.point_cloud2.pointcloud2_to_xyz_array(self.subscriber.pc)
        self.sp3.setData(pos=pc)

启动后,我收到错误消息:

if __name__ == '__main__':

    listener = CameraListner()

    app = QApplication(sys.argv)
    window = ConfigUi(subscriber)

    sys.exit(app.exec_())

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用pyqtgraph byt是我的新手,首先在Qt Designer中放置对象名称为'plotwidget'的QWidget,即可成功地在条形图中绘制值列表。然后将此Qwidget提升为pyqtgraph.PlotWidget类。然后,绘制我的条形图的代码就很简单:

bg = pg.BarGraphItem(x=range(len(value_list)), height=value_list, width=0.8, brush='b')
self.ui.plotwidget.addItem(bg)

我将尝试在Qt Designer中将QWidget升级为GLViewWidget,然后像在代码中使用BarGraphItem一样添加GLScatterPlotItem。

我使用pyqtpgraph 0.11.0rc0和Pyside2 5.13.1