pyqtgraph with Qt Designer:如何将 PlotItems 添加到 GraphicsLayoutWidget

pyqtgraph with Qt Designer: how to add PlotItems to GraphicsLayoutWidget

根据 pyqtgraph's documentation,我可以将 QGraphicsView 升级为 GraphicsLayoutWidget。如果我没记错的话,我还可以将 QGraphicsView 升级为 PlotItem,这就是情节所在。现在我的问题是,如何使用 Qt Designer 添加许多 PlotItem(从 QGraphicsView 提升)到 GraphicsLayoutWidget(也从 QGraphicsView 提升)?

如果重要的话,我正在使用 pyside2。

PlotItem 不是一个小部件,更不用说 QGraphicsView,所以你不能推广 PlotItem(如果你能做到,如果你展示它会很棒)。 PlotItem 是属于 QGraphicsView 处理的场景的项目,正如您所说,它是数据。您必须做的是提升 GraphicsLayoutWidget 并使用代码添加项目。

示例:

design.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="GraphicsLayoutWidget" name="graphicsView"/>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>GraphicsLayoutWidget</class>
   <extends>QGraphicsView</extends>
   <header>pyqtgraph</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

执行:

pyside2-uic design.ui -o design.py -x

design.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'design.ui',
# licensing of 'design.ui' applies.
#
# Created: Mon Oct  8 23:14:34 2018
#      by: pyside2-uic  running on PySide2 5.11.2
#
# WARNING! All changes made in this file will be lost!

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.graphicsView = GraphicsLayoutWidget(Form)
        self.graphicsView.setObjectName("graphicsView")
        self.verticalLayout.addWidget(self.graphicsView)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtWidgets.QApplication.translate("Form", "Form", None, -1))

from pyqtgraph import GraphicsLayoutWidget

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

main.py

from PySide2 import QtWidgets
from design import Ui_Form
import pyqtgraph as pg
import numpy as np


class Widget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.setupUi(self)

        w1 = self.graphicsView.addPlot()
        w2 = self.graphicsView.addViewBox()
        w2.setAspectLocked(True)
        self.graphicsView.nextRow()
        w3 = self.graphicsView.addPlot()
        w4 = self.graphicsView.addPlot()

        n = 300
        s1 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
        pos = np.random.normal(size=(2,n), scale=1e-5)
        spots = [{'pos': pos[:,i], 'data': 1} for i in range(n)] + [{'pos': [0,0], 'data': 1}]
        s1.addPoints(spots)
        w1.addItem(s1)


        s2 = pg.ScatterPlotItem(size=10, pen=pg.mkPen('w'), pxMode=True)
        pos = np.random.normal(size=(2,n), scale=1e-5)
        spots = [{'pos': pos[:,i], 'data': 1, 'brush':pg.intColor(i, n), 'symbol': i%5, 'size': 5+i/10.} for i in range(n)]
        s2.addPoints(spots)
        w2.addItem(s2)

        s3 = pg.ScatterPlotItem(pxMode=False)   ## Set pxMode=False to allow spots to transform with the view
        spots3 = []
        for i in range(10):
            for j in range(10):
                spots3.append({'pos': (1e-6*i, 1e-6*j), 'size': 1e-6, 'pen': {'color': 'w', 'width': 2}, 'brush':pg.intColor(i*10+j, 100)})
        s3.addPoints(spots3)
        w3.addItem(s3)

        s4 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 20))
        pos = np.random.normal(size=(2,10000), scale=1e-9)
        s4.addPoints(x=pos[0], y=pos[1])
        w4.addItem(s4)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

输出: