2017-09-12 879 views
0

我試圖開發一個GUI,允許在PlotWidget中跟蹤鼠標座標,並在主窗口中的其他地方顯示標籤。我曾多次嘗試模擬pyqtgraph文檔中的十字準線示例,但未能同意執行此操作。難度的一部分是我無法理解如何訪問我可以在QtDesigner中啓用的鼠標跟蹤。在pyqtgraph中顯示座標?

我試圖使用:

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類中,還是在它之外?

+0

我有一段時間沒有使用過PyQtGraph,但你似乎在重複你的連接。 'SignalProxy'應該已經等同於'... .sigMouseMoved.connect(...)'。此外,鼠標跟蹤應該發生在pyqtgraph圖部件中。我不認爲你在QtDesigner上激活的其他選項存在任何關係。這種工作方式可能是任何SIGNAL/SLOT工作的方式。每次移動鼠標時SignalProxy都會發送一個信號,並運行您爲連接實施的任何方法。這不是實時的,只能在鼠標移動時才啓動。 – armatita

+0

是的,這似乎是正確的。但是,我無法正確寫入「更新程序」,因此無法連接到信號/插槽輸出端(我只能在移動鼠標時才能確認這一點),這就是當我收到錯誤信息時)更新代碼中的標籤? – Jmegan042

+0

顯然你在移動鼠標時正在訪問該方法。它只是在事後發生(可能在連接的方法內)。什麼是錯誤信息?究竟哪部分代碼崩潰? – armatita

回答

0

我能得到更新通過做工作如下:

IN THE 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]的情況下映射它。