PDA

View Full Version : Scaling a QGraphicsPixmapItem in a QGraphicsScene/View



dcole
18th April 2011, 21:15
Hello,

I have a scene that can have multiple QGraphicsPixmapItems in it, which I have overridden to call ImagePixmapItem.

I want to make it so that the item that has mouse focus will respond to wheel events, allowing a user to scoll up and back with the wheel mouse whcih will zoom the image. This is currently working fine, except that using code like:


qreal factor = 1.2;
if (event->delta() < 0)
factor = 1.0 / factor;

this->setTransform(QTransform::fromScale(factor,factor), true);
this->setScaleFactor(scaleFactor*factor);

Makes the image always zoom from the upper left corner. Since the size of the original images is approx 4000x5000, this can get cumbersome, because if you're viewing the zoomed out version, on say the bottom right corner, every time you zoom in, it shifts down and to the right (since it seems to be scaling from 0,0), and thus you have to zoom, then pan, then zoom, them pan, etc. I want to be able to zoom the image and then translate it so that the the area under the mouse is the same before and after the zoom.

I tried to use a couple of different things like:
1)
this->setTransformOriginPoint(event->pos());
this->setTransform(QTransform::fromScale(factor,factor), true);
2)
this->setTransform(QTransform::fromTranslate(event->scenePos().x(), event->scenePos().y()));
3)
this->setTransform(QTransform::fromTranslate(event->pos().x(), event->pos().y()));

But this sems to give all manner of terrible results.

What is the proper way to do this? I dont want to just zoom the view, beacause that will zoom every image in the scene, and I will have more than one present. Is there a way from wtihin the ImagePixmapItem to zoom under the mouse, performing the proper transformations that will keep the area of interest on the screen while zooming in and out?

Thanks

Derek

dcole
19th April 2011, 15:53
bump..no one has any ideas?