我正在尝试扩大规模
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