I have a need to integrate a zoom slider for a QGraphicsView
in Qt 4.x, I have a working implementation that goes something like this:
connect(slider, SIGNAL(valueChanged(int)), customGraphicsView, SLOT(setZoomLevel(int));
In the slot for setZoomLevel
I have the following
void CustomView::setZoomLevel(int level)
{
if(zoomLevel - level < -1){
setZoomLevel(level - 1);
}else if(level - zoomLevel < -1){
setZoomLevel(level + 1);
}
if(level < zoomLevel){
scale(1 - (scaleFactor * (zoomLevel - level)), 1 - (scaleFactor * (zoomLevel - level)));
}else if (level > zoomLevel){
scale(1 + (scaleFactor * (level - zoomLevel)), 1 + (scaleFactor * (level - zoomLevel)));
}
zoomLevel = level;
}
So my issue is stemming from mating a slider which has a value of n to m to represent a zoom level to the scale()
function of QGraphicsView
, which takes two floating point values to multiply the scene by to get a new size.
So the problem I'm having is, if you take 1 * .9 * 1.1 you do not still get 1 but instead .99, its off slightly because it isn't a correct formula. So my max zoom gets smaller and smaller over time.
The recursive calls are because the slider would sometimes skip values on fast slides, which increased the "error", so I smoothed it out to bandage it a little.
Is there a correct way of handling zooms?