gpt4 book ai didi

python - PyQt4滚动故障

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:43 32 4
gpt4 key购买 nike

我编写了一个小应用程序,它利用 QGraphicsView。您可以缩放和平移 View 。问题是,当您放大(因此 View 将比窗口大)并平移 View 时,会出现以下故障( picture )

我的代码:

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_())

如果你在 mousereleaseevent 中调用 self.scaleView(1.2),小故障就会消失,但只有当事件发生时才会消失。我认为这是因为我没有调用正确的重绘/更新,但我没有找到正确的......

最佳答案

不要更改后台的默认缓存模式 - 如果满足以下条件,您的问题就已解决您删除此行:self.setCacheMode(QtGui.QGraphicsView.CacheBackground)

关于python - PyQt4滚动故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124625/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com