Python 将QGraphicsLineItem添加到pyqt/pyqtgraph中的场景/视图
我正在尝试为pyqtgraph中的图形创建自定义边框。我通过向场景/图形视图(pyqtgraph中的ViewBox)添加QGraphicsLineItem来实现这一点,但这两种方法都有问题。将QGraphicsLineItem添加到场景中会得到我想要的结果(围绕顶部和右侧轴的边框),但它不会缩放:Python 将QGraphicsLineItem添加到pyqt/pyqtgraph中的场景/视图,python,pyqt,pyqtgraph,viewbox,Python,Pyqt,Pyqtgraph,Viewbox,我正在尝试为pyqtgraph中的图形创建自定义边框。我通过向场景/图形视图(pyqtgraph中的ViewBox)添加QGraphicsLineItem来实现这一点,但这两种方法都有问题。将QGraphicsLineItem添加到场景中会得到我想要的结果(围绕顶部和右侧轴的边框),但它不会缩放: upperplot = graphics_layout_widget.addPlot(0, 0, 1, 1) self.curve_upper = upperplot.plot(np.linspace
upperplot = graphics_layout_widget.addPlot(0, 0, 1, 1)
self.curve_upper = upperplot.plot(np.linspace(0,0,8192),
np.linspace(0,0,8192), # loaded_file.data.vm_array[0]
pen=plotpen)
tl = upperplot.getViewBox().boundingRect().topLeft()
tr = upperplot.getViewBox().boundingRect().topRight()
topline = QtGui.QGraphicsLineItem(tl.x(), tl.y(), tr.x(), tr.y())
topline.setParentItem(upperplot.getViewBox())
topline.setPen(pg.mkPen(color=(211, 211, 211), width=10))
upperplot.getViewBox().scene().addItem(topline)
我看到GraphicsView处理所有大小调整,并尝试将该项直接添加到GraphicsView:
upperplot.getViewBox().addItem(topline)
除了直线现在以Y=0为中心,而不是左上角外,其余均有效。有趣的是,X轴是可以的
我觉得这是一个简单的解决方案,但我一生都找不到答案-我不确定这是一个映射场景到视图的问题,还是在viewbox中对齐场景的问题,但我没有成功地尝试过这两种方法。任何帮助都将不胜感激
最小可复制示例:
from PyQt5 import QtWidgets, QtGui
import pyqtgraph as pg
import numpy as np
class UiMainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(UiMainWindow, self).__init__()
# set mainwindow + widgets
self.mainwidget = QtWidgets.QWidget(self)
self.mainwidget_gridlayout = QtWidgets.QGridLayout(self.mainwidget)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.mainwidget_gridlayout)
self.graphics_layout_widget = pg.GraphicsLayoutWidget() # contains a graphicsview
self.graphics_layout_widget.setBackground('w')
pg.setConfigOption('foreground', 'k')
self.mainwidget_gridlayout.addWidget(self.graphics_layout_widget)
# make plot
plotpen = pg.mkPen(color='k', width=1)
self.upperplot = self.graphics_layout_widget.addPlot(0, 0, 1, 1)
self.curve_upper = self.upperplot.plot(np.linspace(0, 100, 8192),
np.linspace(0, 0, 8192),
pen=plotpen)
# draw top border line
QtWidgets.QApplication.processEvents() # I could not get the boundingRect of the ViewBox without drawing first
tl = self.upperplot.getViewBox().boundingRect().topLeft()
tr = self.upperplot.getViewBox().boundingRect().topRight()
br = self.upperplot.getViewBox().boundingRect().bottomRight()
topline = QtGui.QGraphicsLineItem(tl.x(), tl.y(), tr.x(), tr.y())
topline.setParentItem(self.upperplot.getViewBox())
topline.setPen(pg.mkPen(color=(211, 211, 211), width=10))
rightline = QtGui.QGraphicsLineItem(tr.x(), tr.y(), br.x(), br.y())
rightline.setParentItem(self.upperplot.getViewBox())
rightline.setPen(pg.mkPen(color=(211, 211, 211), width=10))
self.upperplot.getViewBox().addItem(topline) # correct scaling, but Y axis is centered as zero
self.upperplot.getViewBox().addItem(rightline)
# vs
# self.upperplot.getViewBox().scene().addItem(topline) # correct position, but cannot scale
# self.upperplot.getViewBox().scene().addItem(rightline)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mw = UiMainWindow()
mw.show()
sys.exit(app.exec_())
关于边界的更新:
self.upperplot.getViewBox().setBorder(color=(211, 211, 211), width=10)
给出:
而不是:
一种可能的解决方案是通过执行自定义绘制来实现自定义ViewBox:
类CustomViewBox(pg.ViewBox):
def油漆(自、p、opt、小部件):
super().paint(p、opt、widget)
r=QtCore.QRectF(self.boundingRect())
p、 保存()
tl=r.左上()
tr=r.topRight()
br=r.右下()
钢笔=pg.mkPen(颜色=(211211211211),宽度=10)
p、 设置笔(笔)
p、 抽绳(tl、tr)
p、 抽绳(tr,br)
p、 还原()
self.upperplot=self.graphics\u layout\u widget.addPlot(
0,0,1,1,viewBox=CustomViewBox()
)
谢谢eyllanesc,请查找添加内容。您可以放置您想要的图片1)删除所有QGraphicsLineItem
并使用self.upperplot.getViewBox().setBorder(颜色=(211211211211211),宽度=10)
谢谢我编辑了文本,以使我的目标更清晰(第一张图片,但在调整窗口大小时进行了适当的调整)。我尝试了类似的方法self.upperplot.getViewBox().border=pg.mkPen(color=(211211211,211),width=10)
(我似乎在ViewBox上找不到方法setOrder)但问题是灰色边框现在覆盖了底部和左轴,我想保持黑色。
self.upperplot = self.graphics_layout_widget.addPlot(
0, 0, 1, 1, viewBox=CustomViewBox()
)