2017-04-10 87 views
1

关于这个问题和答案here,有没有办法将滚动条滚动条传递到滚动条时鼠标位于图上?我试过在主窗口小部件中使用一个事件过滤器,但它没有注册轮子在主窗口中滚动,只在画布/图中显示。我不需要知道它正在滚动的情节,只需要GUI。任何帮助将不胜感激,谢谢。pyqt4 scrollArea事件和matplotlib wheelEvent

回答

0

一种解决方案来滚动FigureCanvas内侧PyQt的一个QScrollArea是使用matplotlib的"scroll_event"(见Event handling tutorial)并将其连接到该滚动QScrollArea的滚动条的功能。

的例子(从我的回答this question)可以延伸通过

self.canvas.mpl_connect("scroll_event", self.scrolling) 

连接到功能scrolling此功能的滚动条值被更新的内部。

import matplotlib.pyplot as plt 
from PyQt4 import QtGui 
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar 

class ScrollableWindow(QtGui.QMainWindow): 
    def __init__(self, fig): 
     self.qapp = QtGui.QApplication([]) 

     QtGui.QMainWindow.__init__(self) 
     self.widget = QtGui.QWidget() 
     self.setCentralWidget(self.widget) 
     self.widget.setLayout(QtGui.QVBoxLayout()) 
     self.widget.layout().setContentsMargins(0,0,0,0) 
     self.widget.layout().setSpacing(0) 

     self.fig = fig 
     self.canvas = FigureCanvas(self.fig) 
     self.canvas.draw() 
     self.scroll = QtGui.QScrollArea(self.widget) 
     self.scroll.setWidget(self.canvas) 

     self.nav = NavigationToolbar(self.canvas, self.widget) 
     self.widget.layout().addWidget(self.nav) 
     self.widget.layout().addWidget(self.scroll) 

     self.canvas.mpl_connect("scroll_event", self.scrolling) 

     self.show() 
     exit(self.qapp.exec_()) 

    def scrolling(self, event): 
     val = self.scroll.verticalScrollBar().value() 
     if event.button =="down": 
      self.scroll.verticalScrollBar().setValue(val+100) 
     else: 
      self.scroll.verticalScrollBar().setValue(val-100) 


# create a figure and some subplots 
fig, axes = plt.subplots(ncols=4, nrows=5, figsize=(16,16)) 
for ax in axes.flatten(): 
    ax.plot([2,3,5,1]) 

# pass the figure to the custom window 
a = ScrollableWindow(fig) 
+0

谢谢你,我不得不做一些变通,但它融合了你的解决方案以及保持欲望的显示和功能。 – ntmt