2017-08-05 66 views
0

我需要在鼠標滾輪事件上調整verticalScrollBar()。試圖獲得與horisontalScrollBar()相同的行爲。我的意思是它應該保持在垂直滾動區域的中心。 下面是代碼:縮放比例調整verticalScrollBar QGraphicsView

#!/usr/bin/env python 
from PySide.QtGui import * 


class windowClass(QWidget): 
    def __init__(self): 
     super(windowClass, self).__init__() 
     self.ly = QVBoxLayout(self) 
     self.view = viewClass() 
     self.ly.addWidget(self.view) 
     self.resize(500, 200) 


class sceneClass(QGraphicsScene): 
    def __init__(self): 
     super(sceneClass, self).__init__() 
     self.setSceneRect(-1000, -1000, 2000, 2000) 
     self.grid = 30 


class viewClass(QGraphicsView): 
    def __init__(self): 
     super(viewClass, self).__init__() 
     self.setDragMode(QGraphicsView.RubberBandDrag) 
     # self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) 
     # self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) 

     self.s = sceneClass() 
     self.setScene(self.s) 

     self.scaleY = 1 
     self.scaleX = 1 

    def wheelEvent(self, event): 
     self.setSceneScale(event.delta()) 

     super(viewClass, self).wheelEvent(event) 

    def setSceneScale(self, delta): 
     if delta > 0: 
      self.scale(self.scaleX + 0.1, self.scaleY + 0.1) 
     else: 
      self.scale(self.scaleX - 0.1, self.scaleY - 0.1) 

if __name__ == '__main__': 
    app = QApplication([]) 
    w = windowClass() 
    w.show() 
    app.exec_() 

正如你可以看到我已經習慣setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)但它並沒有奏效,因爲它只是隱藏滾動條

回答

0

我已經得到了答案。

QGraphicsView構造:

self.vscr = (self.size().height()/2)*-1 

wheelEvent

self.verticalScrollBar().setValue(self.vscr)