Python 当resizeEvent缩放QGraphicsView和QGraphicsSitem时,QGraphicsView和背景中会出现渲染故障

Python 当resizeEvent缩放QGraphicsView和QGraphicsSitem时,QGraphicsView和背景中会出现渲染故障,python,qgraphicsscene,qgraphicsitem,pyside2,Python,Qgraphicsscene,Qgraphicsitem,Pyside2,我正在尝试缩放一个QGraphicsItem,(box),以便它正好适合在QGraphicsView背景中/上绘制的一对线,如下所示 import sys from PySide2.QtCore import Qt from PySide2.QtGui import QPen from PySide2.QtWidgets import QApplication , QGraphicsScene , QGraphicsView class MyView(QGraphicsView): d

我正在尝试缩放一个
QGraphicsItem
,(
box
),以便它正好适合在
QGraphicsView
背景中/上绘制的一对线,如下所示

import sys
from PySide2.QtCore import Qt
from PySide2.QtGui import QPen
from PySide2.QtWidgets import QApplication , QGraphicsScene , QGraphicsView

class MyView(QGraphicsView):
    def drawBackground( self , painter , rect ):
        size = rect.size()

        # Two lines each drawn 10 units from the top and bottom of the view
        painter.drawLine( 0 , 10 , size.width() , 10 )
        painter.drawLine( 0 , size.height() - 10  , size.width() , size.height() - 10 )

    def resizeEvent( self , event ):
        size = event.size()
        box.setPos( 0 , 10 )

        # Box height = 30
        # Target height = size.height()- 20
        # (20 is total gap of lines in background)
        factor = ( size.height()- 20 )/30.0
        #print (factor * 30 ) - (size.height() - 20) # Check: prints 0.0 always
        box.setScale( factor )

if __name__=="__main__":
    app = QApplication(sys.argv)

    view = MyView()
    view.setAlignment( Qt.AlignLeft | Qt.AlignTop )
    view.setVerticalScrollBarPolicy( Qt.ScrollBarAlwaysOff )
    view.setHorizontalScrollBarPolicy( Qt.ScrollBarAlwaysOff )

    scene = QGraphicsScene()
    scene.setSceneRect( 0 , 0 , 300 , 300 )
    box = scene.addRect( 0 , 0 , 10, 30)

    pen = QPen()
    pen.setCosmetic( True )
    box.setPen( pen )

    view.setScene( scene )
    view.show()

    sys.exit(app.exec_())
问题是,当应用程序调整大小时,渲染问题会出现,尤其是在长方体底部和背景中的下一行之间。请参阅下面屏幕截图的圆圈部分

Qt版本5.9.5,PySide2版本5.12.1


运行您的代码时,我没有遇到任何问题。工作完美我用PySide2 5.13在Linux中重现了这个问题,为此我遵循以下步骤:1)启动应用程序,2)逐渐减小窗口大小,您将看到问题出现的高度