2017-05-07 119 views
0

我是一個新的pyqtgraph用戶,請按照http://www.pyqtgraph.org/documentation/how_to_use.html中的說明嘗試「在PyQt應用程序中嵌入小部件」。在我的例子中,我提倡的圖形視圖來PlotWidget,然後另存爲「test2.ui」,也遵循「十字/鼠標交互」的例子,我的代碼:如何獲取鼠標位置

import sys 
import numpy 
from PyQt5 import QtCore, QtGui,uic,QtWidgets 
from PyQt5.QtWidgets import * 
import pyqtgraph as pg 
import os 


hw,QtBaseClass=uic.loadUiType("test.ui") 
def gaussian(A, B, x): 
    return A * numpy.exp(-(x/(2. * B)) ** 2.) 
class MyApp(QtWidgets.QMainWindow, hw): 
    def __init__(self): 
     super().__init__() 
     self.setupUi(self) 
     winSize=self.size() 
     self.view.resize(winSize.width(),winSize.height()) 
     x = numpy.linspace(-5., 5., 10000) 
     y =gaussian(5.,0.2, x) 
     self.p=self.view.plot(x,y) 

     proxy = pg.SignalProxy(self.view.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved) 
     self.view.enableAutoRange("xy", True) 

    def mouseMoved(evt): 
     print("mouseTest") 
     mousePoint = self.p.vb.mapSceneToView(evt[0]) 
     label.setText(
      "<span style='font-size: 14pt; color: white'> x = %0.2f, <span style='color: white'> y = %0.2f</span>" % (
     mousePoint.x(), mousePoint.y())) 

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv) 
    window = MyApp() 
    window.show() 

    sys.exit(app.exec_()) 

似乎沒有得到鼠標移動事件; 後變化 代理= pg.SignalProxy(self.view.scene()。sigMouseMoved,rateLimit = 60,空位= self.mouseMoved) 到 self.view.scene()。sigMouseMoved.connect(self.mouseMoved), 輸出「MouseTest」,但程序imediatly崩潰。 任何一個可以給我一些幫助

回答

0

兩件事情:

回覆:崩潰 看來,如果你還沒有放置標籤在GUI進行修改,或許你的代碼是看到這一點,並踢回給你。如果你使用的是qtDesigner,它可能被定義爲self.label,並且在我的GUI中,我被要求使用self.label來引用它。

回覆:mouseMoved函數 我只是在努力解決一個類似的問題,它不工作。我可以通過將evt[0]更改爲simply evt,我認爲他們從pyqt4轉移到pyqt5。

這裏是什麼,我能得到工作的例子:

..........設置上面的代碼......在setupUi功能:

..........setup code above... 
     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.label.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() 
...the if__name__ =="__main__": function ..... 

在我的情況是,我沒有通過代理聲明,而是隻是去了sigMouseMoved,因爲它已經傳遞了代理的信息。我認爲這是pyqt5中的例子(並註釋掉),因爲這是變化。但是,評論沒有具體說明這一點。