How to resize a QImage or QML Image to fit parent container without breaking the Image's aspect ratio
Asked Answered
G

1

11

Scenario:
I have an Image component in QML which contains a QImage of varied aspect ratios.

Code:

Window {
  id: app_window
  visible: true

  Rectangle {
    id: my_image_view_container
    width: app_window.width
    height: app_window.height

    Image {
       id: my_image
       // changes at runtime based on the image my app selects
       source: "random/path/to/an/image.jpg"
       width: sourceSize.width
       height: sourceSize.height
       scale: Qt.KeepAspectRatio
    }
  }
}

Question:
How do I set the width & height of the my_image_view_container so as to fit my_image completely inside my_image_view_container without disturbing its aspect ratio?

Grenadines answered 12/12, 2017 at 13:41 Comment(0)
M
20

You can use fillMode property. Like this

fillMode: Image.PreserveAspectFit
Meras answered 12/12, 2017 at 13:43 Comment(1)
Exxactly what I was looking for. I knew this property long time ago. Was missing to set this right. thanks a ton !Grenadines

© 2022 - 2024 — McMap. All rights reserved.