在pyqtgraph中显示坐标?

时间:2017-09-12 00:34:18

标签: python-3.x pyqt5 pyqtgraph

我正在尝试开发一个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类中,还是在它之外?

2 个答案:

答案 0 :(得分:0)

通过执行以下操作,我能够获得更新:

在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]的情况下对其进行映射。

答案 1 :(得分:0)

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

Options +FollowSymlinks
RewriteBase /    
RewriteEngine on    
RewriteRule ^page1$ page1.php    
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+FOF/news/page/([^\s]+) [NC]    
RewriteRule ^%1 [R=301,L]   
RewriteCond %{REQUEST_FILENAME} !-f   
RewriteRule (?!^FOF/news/page/)^(.*)$ /FOF/news/page/$1 [L?NC]