Image Resizing in QGraphics



  • hi,

    I'm having some problems when putting an image in a resizable widget. I am subclassing QGraphicsView and inside this class I have a scene and an item subclassed from QGraphicsItem. The GraphicsView widget gets placed in a QSplitter. So when I resize the splitter I want the image inside to also resize accordingly. I managed to do this by using the function

    @
    fitInView(scene->sceneRect, Qt::IgnoreAspectRatio);
    @

    This function is inside the resizeEvent in the QGraphicsView Class.
    Before using this function, I wasn't able to have the image cover the entire view for some reason even though when I draw the image I am doing inside the paint function in QGraphicsItem:
    @

    painter->drawImage(scene->sceneRect, myImage);

    @
    where scene is the scene that is holding the item.

    So now I need to be able to zoom in my image only in width. I implemented this inside the wheelEvent in the QGraphicsView class, when I zoom out it's fine, the image just starts getting smaller (in width) but when I zoom in since the width of the image starts growing I can't zoom in because of the fitInView function implemented in the resizeEvent obviously.

    So my question is how could I have the image always fit in height in the view even when I resize the widget containing it, and also then be able to zoom-in in width, in this case the viewport would only be seeing the zoomed area since the image now is wider than the viewport until I zoom out to the original size. My problem was that without fitInview the image wasn't resizing when the widget gets resized..

    Any help greatly appreciated!

    Thanks.



  • I use transformations. The whole QGraphicsView might be affected or just the item.

    You'll need to reset an item's transformation before applying a new one.

    By the way, does your class inherit from QGraphicsPixmapItem ?

    Hope it helps.
    Francisco



  • Thanks for the reply! No, my item class inherits from QGraphicsItem.
    I don't think I know how to do what you are saying.. could you please explain a little more?



  • Try QGraphisPixmapItem:

    "http://doc.qt.nokia.com/4.7/qgraphicspixmapitem.html#details":http://doc.qt.nokia.com/4.7/qgraphicspixmapitem.html#details

    The easiest way: You can set the scale of the QGraphicsView:

    "http://doc.qt.nokia.com/4.7/qgraphicsview.html#scale":http://doc.qt.nokia.com/4.7/qgraphicsview.html#scale

    The "scale" method uses two factors, one for X and another for Y. As I understand, you will always keep the Y factor as 1.0 .

    But before setting a new scale (a particular case of transformation), you will need to reset the previous transformation:

    "http://doc.qt.nokia.com/4.7/qgraphicsview.html#resetTransform":http://doc.qt.nokia.com/4.7/qtransform.html#scale

    A bit more complicated, as that might be a little bit more slower than really understanding the transformations and applying a calculated one, adjusted for the difference in scale, rather than reseting and setting a new one.

    Create a QTransform and apply to it the scale you need:

    "http://doc.qt.nokia.com/4.7/qtransform.html#scale":http://doc.qt.nokia.com/4.7/qtransform.html#scale

    Then apply it to the QGraphicsView without combining it with the previous transformation:

    "http://doc.qt.nokia.com/4.7/qgraphicsview.html#setTransform":http://doc.qt.nokia.com/4.7/qgraphicsview.html#setTransform

    Your resize event might do this:

    • get new width;
    • calculate the ratio between this and the first ever width;
    • create a QTransform object and set its scale properly;
    • apply this to the QGraphicsView using its setTransform method without combination to the previous;

    OR

    • get new width;
    • calculate the ratio between this and the last one;
    • save width for future use;
    • create a QTransform object and set its scale properly;
    • apply this to the QGraphicsView using its setTransform method combining it to the previous;

    I have never tried to do this, but I guess that will work as expected.

    Hope this helps,
    Francisco


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.