0
我有一個可縮放的QGraphicsView
,場景包含數百(甚至數千)個數據點。積分由QGraphicsEllipseItem
s表示並收集到QGraphicsItemGroup
中。當視圖放大時,我希望數據點保持恆定的大小(即,相鄰點之間的距離增加,但大小保持不變)。現在我通過每一個用戶放大時間運行此代碼實現這一點: 縮放數百個QGraphicItems的高效方法
#get all the QGraphicsEllipseItems that make up the QGraphicsItemGroup
children = graphics_item_group.childItems()
for c in children:
#base_size_x and base_size_y are the sizes of the
#untrasformed ellipse (on the scene) when zoom factor is 1
#New width and height are obtained from the original sizes and
#the new zoom factors (h_scale, v_scale)
new_width = base_size_x/h_scale
new_height = base_size_y/v_scale
#The top-left corner of the new rectangle for the item has to be recalculated
#when scaling in order to keep the center at a constant position
#For this, the center of the item has to be stored first
old_center_x = c.rect().center().x()
old_center_y = c.rect().center().y()
#New coordinates of the rectangle top left point are calculated
new_topleft_x = old_center_x - new_width/2.
new_topleft_y = old_center_y - new_height/2.
#Finally a new rectangle is set for the ellipse
c.setRect(new_topleft_x, new_topleft_y, new_width, new_height)
此代碼的工作。問題在於它非常緩慢(沒有補償縮放放大/縮小非常平滑)。我試圖關閉視圖的抗鋸齒功能,但它使事情看起來非常難看。還有什麼我可以做的,使處理/重繪速度更快?