title: How do I make an image resize to scale in Qt?
body: <p>I made a class called ImageLabel which extends QLabel. I want it to keep the size ratio of the image that it is displaying no matter how stretched out it is. It works fine when you make the window larger. The problem comes in when you try to make the window smaller: it doesnt resize the height, it leaves it stretched out. How do I fix this?</p>
  
  <pre><code>
  int ImageLabel::heightForWidth(int width) const {
      int height = (this->size.height()*width)/this->size.width();
      return height;
  }
  
  QSize ImageLabel::sizeHint() const {
      return this->size;
  }
  
  QSize ImageLabel::minimumSizeHint() const {
      return QSize(0, 0);
  }
  
  void ImageLabel::setSizePolicy(){
      QSizePolicy policy = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
      policy.setHeightForWidth(true);
      QLabel::setSizePolicy(policy);
      QLabel::setScaledContents(true);
  }
  
  void ImageLabel::setPixmap ( const QPixmap &pixmap ){
      this->size = pixmap.size();
      QLabel::setPixmap(pixmap);
  }
  
  int main(int argc, char *argv[]){
      QApplication a(argc, argv);
  
      QFrame *frame = new QFrame;
      QVBoxLayout *layout = new QVBoxLayout;
      frame->setLayout(layout);
  
      QPixmap map;
      map.load("test.png");
      ImageLabel *label = new ImageLabel;
      label->setSizePolicy();
      label->setPixmap(map);
      layout->addWidget(label);
      frame->show();
  
      return a.exec();
  }
  
  </code></pre>