Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, April 2008
zooming in QGraphicsView with mouse as center


Message 1 in thread

I'd like to know how its possible to zoom a scene with a QGraphicsView 
subclass such that it zooms around the mouse cursor. My idea was to translate 
then scale and translate back again but that does not seem to work. In fact 
the translate seems to do nothing at all.

void Viewer::wheelEvent( QWheelEvent * e )
{
    if ( e->modifiers() == Qt::ControlModifier )
    {
        int numSteps = ( e->delta() / 8 ) / 15;

        QMatrix mat = matrix();
        QPointF mousePosition = e->pos();

        mat.translate((width() / 2) - mousePosition.x(), (height() / 2) - 
mousePosition.y());

        if ( numSteps > 0 )
            mat.scale( numSteps * 1.2, numSteps * 1.2 );
        else
            mat.scale( -1 / ( numSteps * 1.2 ), -1 / ( numSteps * 1.2 ) );

        mat.translate(mousePosition.x() - (width() / 2), mousePosition.y() - 
(height() / 2));
        setMatrix(mat);
        e->accept();
    }
    else
        QGraphicsView::wheelEvent( e );
}

I've tried various values for the translate operation to no effect. What's the 
mistake i make?
-- 
 [ signature omitted ] 

Message 2 in thread

kitts wrote:
> I've tried various values for the translate operation to no effect. What's
> the mistake i make?

Have you tried setTransformationAnchor(QGraphicsView::AnchorUnderMouse)?

-- 
 [ signature omitted ] 

Message 3 in thread

On Friday 25 April 2008 12:10:51 Andreas Aardal Hanssen wrote:
> > I've tried various values for the translate operation to no effect.
> > What's the mistake i make?
>
> Have you tried setTransformationAnchor(QGraphicsView::AnchorUnderMouse)?

Thanks! That does just what i want.
-- 
 [ signature omitted ]