缩放QGraphicsView时按比例调整verticalScrollBar

时间:2017-08-05 16:38:06

标签: python pyqt

我需要在鼠标滚轮事件上调整verticalScrollBar()。试图获得与horisontalScrollBar()相同的行为。我的意思是它应该保持在垂直滚动区域的中心。 这是代码:

#!/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)但它没有用,因为它只是隐藏了滚动条

1 个答案:

答案 0 :(得分:0)

我得到了答案。

echo "success";的构造函数中:

QGraphicsView

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

wheelEvent