【问题标题】:Pyqtgraph mouse crosshairPyqtgraph 鼠标十字线
【发布时间】:2022-12-23 05:38:28
【问题描述】:

我想要一个 pyqtgraph (GraphicsView()) 的鼠标十字准线。 我能够使用以下链接中的代码在单独的窗口中创建十字准线:pyqtgraph: add crosshair on mouse_x graph_y,但我无法在 GraphicsView() 中重新创建它。 这是我当前的代码,但它不起作用:

import sys
import pyqtgraph as pg
from PyQt6 import QtCore
from PyQt6.QtWidgets import *


class CrosshairPlotWidget(QWidget):
    """Scrolling plot with crosshair"""

    def __init__(self, parent=None):
        super(CrosshairPlotWidget, self).__init__(parent)

        self.LEFT_X = -10
        self.RIGHT_X = 0

        self.crosshair_plot_widget = pg.PlotWidget()
        # self.crosshair_plot_widget.setXRange(self.LEFT_X, self.RIGHT_X)
        self.crosshair_plot_widget.setLabel('left', 'Price')
        self.crosshair_plot_widget.setLabel('bottom', 'Time')
        self.crosshair_color = (196, 220, 255)

        self.crosshair_plot = self.crosshair_plot_widget.plot()

        self.layout = QGridLayout()
        self.layout.addWidget(self.crosshair_plot_widget)

        self.crosshair_plot_widget.plotItem.setAutoVisible(y=True)
        self.vertical_line = pg.InfiniteLine(angle=90)
        self.horizontal_line = pg.InfiniteLine(angle=0, movable=False)
        self.vertical_line.setPen(self.crosshair_color)
        self.horizontal_line.setPen(self.crosshair_color)
        self.crosshair_plot_widget.setAutoVisible(y=True)
        self.crosshair_plot_widget.addItem(self.vertical_line, ignoreBounds=True)
        self.crosshair_plot_widget.addItem(self.horizontal_line, ignoreBounds=True)

        self.crosshair_update = pg.SignalProxy(self.crosshair_plot_widget.scene().sigMouseMoved, rateLimit=60, slot=self.update_crosshair)

    def update_crosshair(self, event):
        """Paint crosshair on mouse"""

        coordinates = event[0]
        if self.crosshair_plot_widget.sceneBoundingRect().contains(coordinates):
            mouse_point = self.crosshair_plot_widget.plotItem.vb.mapSceneToView(coordinates)
            index = mouse_point.x()
            if self.LEFT_X < index <= self.RIGHT_X:
                self.crosshair_plot_widget.setTitle("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: red'>y=%0.1f</span>" % (mouse_point.x(), mouse_point.y()))
            self.vertical_line.setPos(mouse_point.x())
            self.horizontal_line.setPos(mouse_point.y())

    def get_crosshair_plot_layout(self):
        return self.layout

# Create crosshair plot
crosshair_plot = CrosshairPlotWidget()

app = QApplication(sys.argv)
view = pg.GraphicsView()
l = pg.GraphicsLayout()
view.setCentralItem(l)
view.show()

# cw = QWidget()
# ml = QGridLayout()
# cw.setLayout(ml)
# view.setCentralWidget(cw)

l.addLayout(crosshair_plot.get_crosshair_plot_layout(), 0, 0)

if __name__ == '__main__':
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec()

关于如何做到这一点的任何想法?

【问题讨论】:

  • 如果你也能看看这个,我真的很感激@musicamante

标签: python qt pyqt mouse pyqt6


【解决方案1】:

获取鼠标在视口中的位置,将视口位置转换为场景坐标,将场景坐标转换为绘图坐标,更新垂直线和水平线的位置:

def update_crosshair(self, event):
    view_pos = self.crosshair_plot_widget.mapFromGlobal(QCursor.pos())
    scene_pos = self.crosshair_plot_widget.viewport().mapToScene(view_pos)
    plot_pos = self.crosshair_plot_widget.plotItem.vb.mapSceneToView(scene_pos)
    self.vertical_line.setPos(plot_pos.x())
    self.horizontal_line.setPos(plot_pos.y())

让我知道是否有帮助!

【讨论】:

    猜你喜欢
    • 2013-07-20
    • 2014-05-15
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2020-08-11
    相关资源
    最近更新 更多