【问题标题】:Display coordinates in pyqtgraph?在pyqtgraph中显示坐标?
【发布时间】:2018-02-20 07:34:13
【问题描述】:

我正在尝试开发一个 GUI,允许在 PlotWidget 中跟踪鼠标坐标,并在主窗口其他地方的标签中显示。我曾多次尝试模拟 pyqtgraph 文档中的十字准线示例,但未能让它同意这样做。部分困难是我无法理解如何访问我可以在 QtDesigner 中启用的鼠标跟踪。

我尝试使用:

proxy = pg.SignalProxy(Plotted.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
Plotted.scene().sigMouseMoved.connect(mouseMoved)

但是,我不太明白是什么允许它实时“更新”,也不明白我应该在什么级别上有这个声明。应该

def mouseMoved(evt):
    pos = evt[0]
    if Plotted.sceneBoundingRect().contains(pos):
       mousePoint = vb.mapSceneToView(pos)
       index = int(mousePoint.x())
       if index > 0 and index < len(x):
          mousecoordinatesdisplay.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>" % (mousePoint.x(), y[index], data2[index]))
          vLine.setPos(mousePoint.x())
          hLine.setPos(mousePoint.y()) 

部分代码在 Ui_MainWindow 类中,还是在它之外?

【问题讨论】:

  • 我有一段时间没有使用 PyQtGraph,但您似乎在重复您的连接。 SignalProxy 应该已经等同于 ... .sigMouseMoved.connect(...)。此外,鼠标跟踪应该发生在 pyqtgraph 绘图小部件中。我认为您在 QtDesigner 上激活的其他选项不存在任何关系。这种工作方式很可能是任何 SIGNAL/SLOT 的工作方式。 SignalProxy 每次移动鼠标时都会发送一个信号,并运行您为连接实现的任何方法。这不是实时的,它应该只在鼠标移动时启动。
  • 是的,这似乎是正确的。但是,我无法正确写入“更新程序”,以便与信号/插槽输出的连接(我可以确认仅在移动鼠标时发生,即我收到错误消息时)更新代码中的标签?
  • 显然,当您移动鼠标时,您正在访问该方法。只是之后发生了一些事情(可能在连接的方法内部)。错误信息是什么?究竟哪部分代码崩溃了?
  • 根据您关于它如何准确地传递信息的建议(我想我没有正确考虑它),我找到了一个解决方法,我可以通过省略来让它工作代理声明,并摆脱 pos = evt[0]

标签: python-3.x pyqt5 pyqtgraph


【解决方案1】:

我能够通过执行以下操作使更新正常工作:

在 setupUi 函数中:

Plotted = self.plot
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
Plotted.addItem(vLine, ignoreBounds=True)
Plotted.addItem(hLine, ignoreBounds=True)
Plotted.setMouseTracking(True)
Plotted.scene().sigMouseMoved.connect(self.mouseMoved)

def mouseMoved(self,evt):
        pos = evt
        if self.plot.sceneBoundingRect().contains(pos):
            mousePoint = self.plot.plotItem.vb.mapSceneToView(pos)
            self.mousecoordinatesdisplay.setText("<span style='font-size: 15pt'>X=%0.1f, <span style='color: black'>Y=%0.1f</span>" % (mousePoint.x(),mousePoint.y()))
        self.plot.plotItem.vLine.setPos(mousePoint.x())
        self.plot.plotItem.hLine.setPos(mousePoint.y()

.mousecoordinatedisplay 是一个标签。我花了很长时间才弄清楚如何在设计师的 GUI 中使用它。似乎在 pyqt4 和 pyqt5 之间,Qpointf 发生了变化,新的 Qpointf 不允许索引。只需传递evt 变量,就可以在不调用evt[0] 的情况下映射它。

【讨论】:

  • 您可能希望更改 mouseMoved 函数,以便在 if 语句之前计算 mousePoint 以避免在未初始化的情况下使用它的错误。或者使行位置更新成为 if 语句的“真”子句的一部分。
【解决方案2】:

修改了 MouseMoved 函数以限制我的应用程序显示数字的精度:

def MouseMoved(self, evt):
    pos = evt
    if self.graphWidget.sceneBoundingRect().contains(pos):
        mousePoint = self.graphWidget.plotItem.vb.mapSceneToView(pos)
        x = float("{0:.3f}".format(mousePoint.x()))
        y = float("{0:.3f}".format(mousePoint.y()))
        self.xylabel.setText(f"Cursor Position: {x, y}")

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 2020-11-20
    • 2019-12-02
    • 2018-02-25
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多