如何在 Designer-qt5 中推广的 PlotWidget() 上制作十字线鼠标跟踪器

Posted

技术标签:

【中文标题】如何在 Designer-qt5 中推广的 PlotWidget() 上制作十字线鼠标跟踪器【英文标题】:how to make cross hair mouse tracker on a PlotWidget() promoted in designer-qt5 【发布时间】:2019-09-27 02:45:18 【问题描述】:

我正在尝试在我的 pyqtgraph 交互式绘图上做一个十字准线,多亏了 Designer-qt5,这些绘图嵌入在 PyQt5 GUI 中。我找到了一个工作 pyqtgraph“示例”中的代码。下面发布了一个简化的工作示例。现在我也想要一样,但问题似乎是我提升了一个 QGraphicsView() 到设计器中的 pg.PlotWidget,而不是 pg.GraphicsWindow()?代码对我不起作用,因为我的 p1 是“pyqtgraph.widgets.PlotWidget.PlotWidget 对象”,而在示例中 p1 是 “pyqtgraph.graphicsItems.PlotItem.PlotItem.PlotItem 对象”。

那么我应该怎么做才能让这个例子适合我呢?

import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.Point import Point

pg.setConfigOption('background', '#ffffff')
pg.setConfigOption('foreground', 'k')
pg.setConfigOptions(antialias=True)  

app = QtGui.QApplication([])
win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: crosshair')
label = pg.LabelItem(justify='right')
win.addItem(label)
p1 = win.addPlot(row=1, col=0)       

p1.setAutoVisible(y=True)

#create numpy arrays
#make the numbers large to show that the xrange shows data from 10000 to all the way 0
data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)

p1.plot(data1, pen="r")

#cross hair
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
p1.addItem(vLine, ignoreBounds=True)
p1.addItem(hLine, ignoreBounds=True)

vb = p1.vb 

print(p1)
print(vb)

def mouseMoved(evt):
    pos = evt[0]  ## using signal proxy turns original arguments into a tuple
    if p1.sceneBoundingRect().contains(pos):
        mousePoint = vb.mapSceneToView(pos)
        index = int(mousePoint.x())
        if index > 0 and index < len(data1):
            label.setText("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index]))
        vLine.setPos(mousePoint.x())
        hLine.setPos(mousePoint.y())

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

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

【问题讨论】:

【参考方案1】:

我很抱歉的噪音!我自己修!

重要的部分是:

plot_wg.proxy = 代理

很简单...

以下是适用于任何 PlotWidget 的函数:

def cross_hair(self, plot_wg, log=False ):
    global fit 
    ################### TETS cross hair ############3    

    vLine = pg.InfiniteLine(angle=90, movable=False)#, pos=0)
    hLine = pg.InfiniteLine(angle=0,  movable=False)#, pos=2450000)
    plot_wg.addItem(vLine, ignoreBounds=True)
    plot_wg.addItem(hLine, ignoreBounds=True)


    vb = plot_wg.getViewBox()


    label = pg.TextItem()
    plot_wg.addItem(label) 

    def mouseMoved(evt):
        pos = evt[0]  ## using signal proxy turns original arguments into a tuple
        if plot_wg.sceneBoundingRect().contains(pos):


            mousePoint = vb.mapSceneToView(pos)

            if log == True:
                label.setText("x=%0.3f,  y1=%0.3f"%(10**mousePoint.x(), mousePoint.y()))
            else:
                label.setText("x=%0.3f,  y1=%0.3f"%(mousePoint.x(), mousePoint.y()))

            vLine.setPos(mousePoint.x())
            hLine.setPos(mousePoint.y())
            #print(mousePoint.x(),mousePoint.y())

    plot_wg.getViewBox().setAutoVisible(y=True)

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




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

 plot_wg.proxy = proxy

 ################### TETS cross hair ############3 

【讨论】:

以上是关于如何在 Designer-qt5 中推广的 PlotWidget() 上制作十字线鼠标跟踪器的主要内容,如果未能解决你的问题,请参考以下文章

如何在百度中进行网络推广操作

App推广如何对推广渠道进行统计?

如何做好应用商店APP运营推广?

网站推广优化(SEO,网站关键字优化,怎么优化网站,如何优化网站关键字)

网站推广优化教程100条(SEO,网站关键字优化,怎么优化网站,如何优化网站关键字)

如何在 Google Play 应用中推广应用 [关闭]