如何解决PyQtGraph禁用鼠标滚轮,但保持鼠标的其他功能不变
我有一个带有图像的pyqtgraph.ViewBox小部件。启用了鼠标,因此我可以在图像上选择一个矩形,它将被缩放。但是有一个不需要的功能,缩放也会在鼠标滚轮滚动时发生。
如何仅禁用鼠标滚轮,而其余部分保持原样?
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:
$ git update-index --assume-unchanged "temp_*.xml"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。