image on QWidget resize and keep aspect ratio

  • Hi,

    I see this question over and over, and now I it is my time: is thre some type of QWidget with click slot, that displays an image, and when the widget resizes, so does the image, but the image keeps its aspect ratio, I mean, if it is a circle, it stays a circle, never becoming an ellipse?

    I read, and, but I was not able to implement the suggestion in the last two URLs, as the code I wrote caused the resize event to be called forever.

    I wrote an algorithm that (I think) calculates the sizes of the image, based on the new size of the widget, in order to keep the aspect ratio. But it would go in the resizeEvent method, that, in my experience, as I said above, caused the resize event to be called repeatedly.

    Is there a good solution for this, or even a property in QPushButton (or any other widget), that I am not aware of?

    Thanks a lot!

  • Moderators

    a solution via the resize-event is never correct, since a layout is most probably responsible for the size.
    The most universal approach would be to do the painting of the image (best-fit) yourself. Independant of the actual widget size ratio.

    Or you can try to do the following (untested):

         QSizePolicy sp = this->sizePolicy();
             sp.setHorizontalPolicy( QSizePolicy::Preferred );
             sp.setVerticalPolicy( QSizePolicy::Preferred );
             sp.setHeightForWidth( true );
         this->setSizePolicy( sp );
    int MyWidget::heightForWidth( int width )
        if( this->layout() )
             return BaseClass::heightForWidth( width );
        int width = ...; // calculate the width based on the height in same aspect ratio of the image
        return width;

Log in to reply

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