0

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

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

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

4

1 回答 1

0

在 QGraphicsItem 的构造函数中:

    setFlag(ItemIgnoresTransformations);

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

于 2014-02-12T03:37:10.987 回答