首页 > 解决方案 > 在QGraphicsView的wheelEvent中,self.scale不允许self.scrollContentsBy工作

问题描述

a 的内容的缩放滚动QGraphicsView都应用于wheelEvent. 问题是滚动
只有在. 在缩放滚动调用之间 应用没有成功。wheelEvent
self.update()

额外的点和猜测
如果注释掉滚动部分比例在它的位置上准确地工作,没有移动多次上下滚动但滚动部分没有注释掉,多次滚动(多次放大和缩小)有一点位移观点已被观察到。
调用 ui 元素的触发方法似乎存在冲突。

感谢您分享的知识

# importing libraries
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
import sys

class Scene(QGraphicsScene):
    def __init__(self, _scene_width, _scene_height):
        super(Scene, self).__init__()
        self.setSceneRect(0, 0, _scene_width, _scene_height)
        map_center = _scene_width / 2
        te = QTextEdit()
        te.insertPlainText("Test Text")
        te.setGeometry(map_center, map_center, 100, 100)
        self.addWidget(te)


class View(QGraphicsView):
    def __init__(self, _scene_width, _scene_height):
        super(View, self).__init__()
        self.scale_factor = 1
        self.scene_width = _scene_width
        self.scene_height = _scene_height
        scene = Scene(_scene_width, _scene_height)
        self.setScene(scene)
        self.showMaximized()

    def wheelEvent(self, event):
        if self.underMouse():

            # scale part which doesn't let the scroll part to work
            if event.angleDelta().y() > 0:
                self.scale_factor = self.scale_factor * 1.2
                self.scale(1.2, 1.2)
            else:
                self.scale_factor = self.scale_factor / 1.2
                self.scale(1 / 1.2, 1 / 1.2)

            # scroll part which works fine if the scale part is commented out
            self.scrollContentsBy(10, 0) # values are just for testing


def main():
    app = QApplication(sys.argv)
    view = View(1000000, 1000000)
    view.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

标签: pythonpyqt5scalepyside2qgraphicsview

解决方案


推荐阅读