我可以在pyqtgraph(pyqt5)中用两个图形共享十字准线

时间:2018-05-24 14:54:00

标签: python python-3.x pyqt5 pyqtgraph

我在pyqt5中使用pyqtgraph实现了两个图形,并希望在两个图形之间共享光标。

我的计划结果如下: enter image description here

我想分享十字准线: enter image description here

pyqtgraph能做到吗?非常感谢。

以下是我的代码(更新),它将显示2个图形,光标只允许在graph2中移动。但是,我希望光标可以移动并从graph1和graph2获取数据。

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from pyqtgraph import MultiPlotWidget


class GUI_create_main_window(QWidget):
    def __init__(self):
        super().__init__()

        main_layout = QVBoxLayout(self)

        self.plot1 = pg.PlotWidget()
        main_layout.addWidget(self.plot1)

        self. plot2 = pg.PlotWidget()
        main_layout.addWidget(self.plot2)

        self.draw_cursor()

        self.setLayout(main_layout) 
        self.show()

    #hair cross event    
    def eventFilter(self, source, event):
        try:
            if (event.type() == QtCore.QEvent.MouseMove and
                source is self.plot2.viewport()):
                pos = event.pos()
                if self.plot2.sceneBoundingRect().contains(pos):
                    mousePoint = self.vb.mapSceneToView(pos)
                    self.vLine.setPos(mousePoint.x())
                    self.hLine.setPos(mousePoint.y())

            return QtGui.QWidget.eventFilter(self, source, event)
        except Exception as e:
            traceback.print_exc()
            err = sys.exc_info()[1]
            print(str(err))

    def draw_cursor(self):
        #cross hair
        self.vLine = pg.InfiniteLine(angle=90, movable=False, pen=pg.mkPen('k', width=1))
        self.hLine = pg.InfiniteLine(angle=0, movable=False, pen=pg.mkPen('k', width=1), label='{value:0.1f}',
                                        labelOpts={'position':0.98, 'color': (200,0,0), 'movable': True, 'fill': (0, 0, 200, 100)})
        self.plot2.addItem(self.vLine, ignoreBounds=True)
        self.plot2.addItem(self.hLine, ignoreBounds=True)
        self.vb = self.plot2.plotItem.vb

        #set mouse event
        self.plot2.setMouseTracking(True)
        self.plot2.viewport().installEventFilter(self)

if __name__ == '__main__':
    pg.setConfigOption('background', 'w')
    pg.setConfigOption('foreground', 'k')

    app = QApplication(sys.argv)
    gui = GUI_create_main_window()
    currentExitCode = app.exec_()

0 个答案:

没有答案