PDA

View Full Version : WheelEvent reimplementation of a class derived from QGraphicsView



Sergex
29th August 2011, 16:42
Hello,

I reimplemented the wheelEvent in my graphicsView class to be able to zoom in and zoom out smoothly in width of an image. At first the implementation was:



float exp = (event->delta() / 240.0);
float factor = powf(3/4, exp);
scale(factor, 1.0);


This works great, except that I don't want to be able to zoom out too much that the image starts being smaller than the widget that contains it. I want the container widget rectangle to be the minimum width of the image.

For this I tried using transforms the following way:
In the wheel event I did this instead:
(item is my GraphicsItem object)



//just changed the line : scale(factor, 1.0) to:
item->setScale(factor);


and in my class derived from QGraphicsItem I added to methods:


void setScale(float factor);
void updateTransform();

//In their implementations I did:

void mygraphicsItem::setScale(float factor)
{
m_factor = factor;
updateTransform();
}

void mygraphicsItem::updateTransform()
{
QTransform transform;
transform.scale(m_factor, 1.0);
setTransform(transform);
}

Now the part to prevent and control the zooming is not implemented yet since I am jsut testing this, but shouldn't something like this do the same thing as before when I was scaling the view instead of the item?

Am I remotely on the right track to accomplish what I want? How can this be done?

Thanks!

This last way does nothing like before,

wysota
29th August 2011, 17:44
Isn't setScale an already existing non-virtual method of QGraphicsItem?

Sergex
29th August 2011, 17:55
Isn't setScale an already existing non-virtual method of QGraphicsItem?

Ok yes, it is. So I changed it to set setItemScale. Its not my point to overload the already existent function.

wysota
29th August 2011, 17:57
It wouldn't work anyway, would it?

Sergex
29th August 2011, 18:08
If it did I wouldn't be posting in this forum now would I? Why bother with the useless rhetorical questions..

wysota
29th August 2011, 18:26
I mean trying to override the method wouldn't work. Your method simply could never be called.

I admit I have no idea what you are trying to do, it seems obvious to try it this way:


void Cls::wheelEvent(...) {
qreal sc = item->scale();
if(wheel goes up) sc+= 0.1; // or whatever
else if(wheel goes down) sc -= 0.1;
item->setScale(qBound(0.1, sc, 2.0));
}