缩放数百个QGraphicItem的有效方法

时间:2013-07-08 11:07:08

标签: pyqt pyqt4

我有一个可缩放的QGraphicsView场景,其中包含数百(甚至数千)个数据点。这些点由QGraphicsEllipseItem s表示,并收集到QGraphicsItemGroup中。当视图放大到我希望数据点保持恒定大小时(即,相邻点之间的距离增加但尺寸保持不变)。现在,我通过每次用户放大时运行此代码来实现此目的:

#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)

此代码有效。问题是它很慢(没有补偿性缩放放大/缩小工作非常顺利)。我尝试关闭视图的抗锯齿功能,但它让事情看起来很难看。我还能做些什么来加快/重绘速度吗?

1 个答案:

答案 0 :(得分:0)

在QGraphicsItem的构造函数中:

    setFlag(ItemIgnoresTransformations);

缩放时,该项目将保持相同的大小,您无需手动缩放。

相关问题