Display image in Qt to fit label size

前端 未结 7 1188
[愿得一人]
[愿得一人] 2021-02-01 02:53

I already tried several methods on displaying an image on a form, but none of them works how I would like.

I\'ve read many places that the easiest way is to create a la

7条回答
  •  隐瞒了意图╮
    2021-02-01 03:30

    Actually there is a very simple solution for this problem. There are two things you should modify:

    1. Set the scaled content to true (mentioned above)
    2. Set the label's size policy to ignored

      QLabel lblImage;
      
      lblImage->setPixmap( QPixmap( "big_image.jpg" ) );
      
      lblImage->setScaledContents( true );
      
      lblImage->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
      

    If the lblImage is resizing automatically, the image will stretch to the size of the label.

提交回复
热议问题