【问题标题】:PyQtGraph PolyLineROI maxBounds Not WorkingPyQtGraph PolyLineROI maxBounds 不工作
【发布时间】:2022-01-22 10:17:17
【问题描述】:

我在PlotItem 中有一个PolyLineROI,并试图将手柄限制为仅在绘图范围内移动。我曾尝试使用 maxBounds 参数,但这不起作用(句柄仍然移动到图形之外):

代码:

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore

app = pg.mkQApp('Plot')

window = pg.GraphicsLayoutWidget(show=True, size=(400,400), border=False, title='Plot')
plt = window.addPlot(title='Plot')

polyline = pg.PolyLineROI(
    [[0,0], [10,10], [10,30], [30,10]],
    closed=False,
    maxBounds=QtCore.QRectF(0,0,30,30)
)

plt.addItem(polyline)

plt.disableAutoRange('xy')
plt.autoRange()

if __name__ == "__main__":
    pg.exec()

问题:

我在这些邮件列表中看到了这个问题:

  1. Google Group
  2. Mail Archive Google Group

但他们没有任何答案,这里也没有问过这个问题。

如何防止句柄移动到图形边界之外?

【问题讨论】:

    标签: plot handle polyline pyqtgraph


    【解决方案1】:

    为此,您必须继承 PolyLineROI 并覆盖 checkPointMove

    代码:

    import pyqtgraph as pg
    from pyqtgraph.Qt import QtCore
    
    class GraphPolyLine(pg.PolyLineROI):
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
        def checkPointMove(self, handle, pos, modifiers):
            if self.maxBounds is not None:
                pt = self.getViewBox().mapSceneToView(pos)
            
                if not self.maxBounds.contains(pt.x(), pt.y()):
                    return False
    
            return True
    
    app = pg.mkQApp("ROI Examples")
    
    window = pg.GraphicsLayoutWidget(show=True, size=(400,400), border=False, title='Plot')
    
    plt = window.addPlot(title="Plot")
    plt.setMouseEnabled(False, False)  # Disable zoom and pan
    
    polyline = GraphPolyLine(
        [[0,0], [10,10], [10,30], [30,10]],
        closed=False,
        maxBounds = QtCore.QRectF(0, 0, 30, 30)
    )
    
    plt.addItem(polyline)
    plt.disableAutoRange('xy')
    plt.autoRange()
    
    if __name__ == "__main__":
        pg.exec()
    

    解决方案:

    来自pyqtgraph.graphicsItems.ROI,我们看到

    【讨论】:

      猜你喜欢
      • 2017-01-27
      • 2020-01-30
      • 2018-05-26
      • 2023-03-15
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2021-10-21
      • 2021-10-15
      相关资源
      最近更新 更多