简体   繁体   中英

PyQt4 scroll glitch

I've written a little app, which draws on a QGraphicsView. You can zoom and pan the view. The problem is that when you zoom in (so the view will be bigger than the window), and you pan the view, the flollowing glitch shows( picture )

My code:

import sys
import math
from PyQt4 import QtGui, QtCore



class GraphWidget(QtGui.QGraphicsView):
    def __init__(self):
        super(GraphWidget, self).__init__()
        scene = QtGui.QGraphicsScene(self)
        scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
        scene.setSceneRect(0, 0, 400, 400)
        self.setScene(scene)
        self.setCacheMode(QtGui.QGraphicsView.CacheBackground)
        self.setViewportUpdateMode(QtGui.QGraphicsView.BoundingRectViewportUpdate)
        self.setRenderHint(QtGui.QPainter.Antialiasing)
        self.setResizeAnchor(QtGui.QGraphicsView.AnchorViewCenter)
        self.sFactor=1
        self.scale(1, 1)
        self.setMinimumSize(400, 400)
        self.setWindowTitle("test")
        self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)        
        self.setViewportUpdateMode(QtGui.QGraphicsView.FullViewportUpdate)
        #print self.viewportUpdateMode()

    def keyPressEvent(self, event):
        key = event.key()
        if key == QtCore.Qt.Key_Plus:
            self.scaleView(1.2)
        elif key == QtCore.Qt.Key_Minus:
            self.scaleView(1 / 1.2)
        else:
            super(GraphWidget, self).keyPressEvent(event)

    def wheelEvent(self, event):
        self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
        self.scaleView(math.pow(2.0, -event.delta() / 240.0))

    def drawBackground(self, painter, rect):
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        pen = QtGui.QPen(QtCore.Qt.black, 2/self.sFactor, QtCore.Qt.SolidLine)
        painter.setPen(pen)
        line=painter.drawLine(0, 0, 300, 200)

    def mouseReleaseEvent(self, event):
        #self.update()
        #self.viewport().repaint()
        self.viewport().update()
        #self.scaleView(1.2)
        super(GraphWidget, self).mouseReleaseEvent(event)

    def scaleView(self, scaleFactor):
        self.sFactor = self.matrix().scale(scaleFactor, scaleFactor).mapRect(QtCore.QRectF(0, 0, 1, 1)).width()

        if self.sFactor < 0.07 or self.sFactor > 100:
            return
        self.scale(scaleFactor, scaleFactor)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    widget = GraphWidget()
    widget.show()
    sys.exit(app.exec_())

if you call self.scaleView(1.2) in the mousereleaseevent, the glich disappears, but only when the event occurs. I think it's because I don't call the right repaint/update, but I did not find the right one...

请勿更改后台的默认缓存模式-如果删除以下行,则可以解决问题: self.setCacheMode(QtGui.QGraphicsView.CacheBackground)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM