Issue with fitInView of QGraphicsView when ItemIgnoresTransformations is on

前端 未结 1 1707
庸人自扰
庸人自扰 2021-01-21 16:45

While trying to implement a scene where item sizes do not change but distances between items get magnified I encountered this problem with the following code which draws a recta

相关标签:
1条回答
  • 2021-01-21 17:37

    In fact, the rectangle does not vanish. But it moves around "strangely".

    self.itemB = QtGui.QGraphicsRectItem(30, 50, 20, 20)
    

    This line may not be what you want. This creates an item and puts a rectangle/square starting from (30, 50) in local coordinates. Then you add this to the scene. This gives you an item anchored at (0, 0), spans up to (50, 70) but draws a rectangle only in the bottom right 20x20.

    When you set ItemIgnoresTransformations, item can't do its regular transformations in case of a zoom. Scene zooms in, for item to ignore this transformation, it kind of "shrinks" itself. But it's still anchored at (0, 0) and the rectangle is at the bottom-right, so the drawn rectangle moves toward the upper-left.

    Solution is rather simple. Don't create your rectangle in local coordinates, i.e. your rectangle should start from (0, 0) and you should position it explicitly. Which translates to this:

    self.itemB = QtGui.QGraphicsRectItem(0, 0, 20, 20)
    self.itemB.setPos(30, 50)
    
    0 讨论(0)
提交回复
热议问题