如何调整QImage或QML Image的大小以适合父容器而不破坏Image的长宽比

Gam*_*ads 1 qt image qimage qml qtquick2

场景:
我在QML中有一个Image组件,其中包含一个纵横比不同的QImage

码:

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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

问:
我如何设定widthheightmy_image_view_container,以适应my_image完全内my_image_view_container不影响到其长宽比?

And*_*nov 5

您可以使用fillMode属性。像这样

fillMode: Image.PreserveAspectFit
Run Code Online (Sandbox Code Playgroud)