Qt World Summit: Submit your Presentation

Using the Scale function of QTransform

  • Hello,

    I am looking to have some control of the scaling of a QImage I am displaying on a QGraphicsView. I implemented zooming functionality so you can zoom in and out of the image. For this I am using transforms in the following way:

    //implemented in a zoom method in my QGraphicsClass

    QTransform transform;
    transform = m_transform.fromScale(scaleValue, 1.0);
    m_transform = transform;

    if( -- someCondition -- )
        setTransform(m_transform, true);
       //Debugging code
        qDebug() << "VIEW" << width();
        qDebug() << "SCENE" << channelScene->width();
        qDebug() << "ITEM" << item->boundingRect().width();
        //don't zoom


    In the above code right now I have if(true).. so the 'if' statement always executes so the image zooms in and out fine. What I want to restrict is that the image never gets smaller than the widget that contains it. So I need to find something to put in the condition of the if.
    I tried so far using m11() of the transform or using the boundingRect of the image but nothing seems to work.

    The debug code above which is the width of the view, the scene and the item NEVER change after applying the scaling transformation, so I can't find a way to restrict the zoom out to the point where it fits exactly in the container widget.
    My question is what does the setTransform in the Graphicsview scale? I thought it scales the view but then why when I do the debugging the width of the view never changes??
    Does anyone have some tips or ideas I can use to get control and never go under a particular value when zooming out?

  • That's because those measurements are in local coordinates. ie the item width never changes in its own coordinate system.

    I think you need to transform the item's bounding rect by the total transformation and see if the resulting rect is small than the view's viewport size.

Log in to reply