PyQtGraph 禁用鼠标滚轮,但不影响鼠标的其他功能

PyQtGraph disable mouse wheel, but leave other functions of the mouse untouched

我有一个 pyqtgraph.ViewBox 小部件,里面有一张图片。鼠标已启用,所以我可以 select 在图像上画一个矩形,它会被缩放。但是有一个不需要的功能,缩放也会发生在鼠标滚轮滚动上。

如何只禁用鼠标滚轮,而其余的保持原样?

from PyQt5.QtWidgets import QApplication, QMainWindow
import pyqtgraph as pg
import cv2


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        pg.setConfigOption('imageAxisOrder', 'row-major')
        pg.setConfigOption('leftButtonPan', False)  # if False, then dragging the left mouse button draws a rectangle
    
        self.grid = pg.GraphicsLayoutWidget()
        self.top_left = self.grid.addViewBox(row=1, col=1)

        image = cv2.imread('/path/to/your/image.jpg', cv2.IMREAD_UNCHANGED)
        self.image_item = pg.ImageItem()
        self.image_item.setImage(image)
        self.top_left.addItem(self.image_item)

        self.setCentralWidget(self.grid)


def main():
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()

您可以在 ViewBox 上安装事件过滤器并捕获鼠标滚轮事件,在本例中 QEvent.GraphicsSceneWheel

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QEvent
import pyqtgraph as pg
import cv2


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        pg.setConfigOption('imageAxisOrder', 'row-major')
        pg.setConfigOption('leftButtonPan', False)  # if False, then dragging the left mouse button draws a rectangle
    
        self.grid = pg.GraphicsLayoutWidget()
        self.top_left = self.grid.addViewBox(row=1, col=1)
        self.top_left.installEventFilter(self)

        image = cv2.imread('/path/to/your/image.jpg', cv2.IMREAD_UNCHANGED)
        self.image_item = pg.ImageItem()
        self.image_item.setImage(image)
        self.top_left.addItem(self.image_item)

        self.setCentralWidget(self.grid)

    def eventFilter(self, watched, event):
        if event.type() == QEvent.GraphicsSceneWheel:
            return True
        return super().eventFilter(watched, event)

它比接受的答案简单得多。在 ViewBox 上使用 setMouseEnabled:

from PyQt5.QtWidgets import QApplication, QMainWindow
import pyqtgraph as pg
import cv2


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        pg.setConfigOption('imageAxisOrder', 'row-major')
        pg.setConfigOption('leftButtonPan', False)  # if False, then dragging the left mouse button draws a rectangle

        self.grid = pg.GraphicsLayoutWidget()
        self.top_left = self.grid.addViewBox(row=1, col=1)
        self.top_left.setMouseEnabled(x=False, y=False)  # <--- Add this line

        image = cv2.imread('/path/to/your/image.jpg', cv2.IMREAD_UNCHANGED)
        self.image_item = pg.ImageItem()
        self.image_item.setImage(image)
        self.top_left.addItem(self.image_item)

        self.setCentralWidget(self.grid)


def main():
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()