2017-04-10 88 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