0
votes

I have subclassed QGraphicsView to have some wheel events that make it zoom in and out. When the wheel is scrolled the zoom function is called which scales the GraphicsView

if(zoom_in)
{
    factor = 2.0;
}
else
{
    factor = 0.5;
}

this->scale(factor, factor);

This works fine and scales the items that are on the scene that is set to the graphicsview (which is the functionality i want).

However i also have a QWidget added to this scene. The widget should basically be the background of the QGraphicsView it has the same dimensions and resizes to fit the view whenever the view is resized.

Im aware that you can set ignore translations and have done this

proxyWidget = this->scene()->addWidget(myBackgroundWidget);
proxyWidget->setFlag(QGraphicsItem::ItemIgnoresTransformations);

so when the view is scaled the widget doesnt change size, which is correct, but it does move for some reason, it shift up and to the left on zoom in and down and to the right on zoom out.

I dont get why this is happening at all, because if just remove the ItemIgnoresTransformations flag and zoom in and out it remains perfectly in the centre of the screen but changes size which is obviously not what i want

If anyone can shed some light on this i would appreciate any help

2

2 Answers

1
votes

In fact what you observe is normal. If you don't scale the item, it will "seem" to translate, because all the graphic view is scaled.

A way to reach your goal may be to define a root QGraphicsItem, with your widget parented to it. In function of the bounding rect of the root item, you will just have to recenter the static widget in it to avoid this impression of translation.

1
votes

IMO you should not touch QGraphicsView. The best way is to have some root QGraphicsItem (parent of all items which should be scaled).
Than in scene you can have items which are not scaled (top most parent is not scaled) and items which top most parent is handling mouse wheel events and performs scaling.


In fact origin of your widget doesn't move (remains at same position). Problem is that center of view is most probably center of scaling and your widget is attached by his top left corner to top left corner.
So to fix the problem you may enforce origin of item with flag ItemIgnoresTransformations is in center of scaling. So add fake QGraphicsItem with flag ItemIgnoresTransformations in center of scaling and make it parent for your widget. Most probably you have to set position of the widget to: widget.setPos(widget.width()/2, widget.height()/2).