带有 Qt Designer 的 pyqtgraph:如何将 PlotItems 添加到 GraphicsLayoutWidget
Posted
技术标签:
【中文标题】带有 Qt Designer 的 pyqtgraph:如何将 PlotItems 添加到 GraphicsLayoutWidget【英文标题】:pyqtgraph with Qt Designer: how to add PlotItems to GraphicsLayoutWidget 【发布时间】:2019-03-13 18:20:33 【问题描述】:根据pyqtgraph's documentation,我可以将QGraphicsView
提升为GraphicsLayoutWidget
。如果我没记错的话,我也可以将QGraphicsView
升级为PlotItem
,即情节所在的位置。现在我的问题是,如何使用 Qt Designer 将许多 PlotItem
s(从 QGraphicsView
提升)添加到 GraphicsLayoutWidget
(也从 QGraphicsView
提升)?
我正在使用 pyside2,如果这很重要的话。
【问题讨论】:
【参考方案1】:PlotItem 不是一个小部件,更不用说 QGraphicsView,所以你不能推广 PlotItem(如果你能做到,如果你展示它会很棒)。 A 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_())
输出:
【讨论】:
以上是关于带有 Qt Designer 的 pyqtgraph:如何将 PlotItems 添加到 GraphicsLayoutWidget的主要内容,如果未能解决你的问题,请参考以下文章
如何创建自定义小部件并在 Qt Designer 中使用它?
PyQt5 图形界面 - Qt Designer设置简体中文方法演示,Qt Designer字体设置,Qt Designer工具单独安装包获取,Qt Designer简体中文语言包获取