【问题标题】:How to resize a QImage or QML Image to fit parent container without breaking the Image's aspect ratio如何在不破坏图像纵横比的情况下调整 QImage 或 QML 图像的大小以适应父容器
【发布时间】:2017-12-12 13:41:48
【问题描述】:

场景:
我在 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
    }
  }
}

问题:
如何设置my_image_view_containerwidthheight 以便将my_image 完全放入my_image_view_container 而不影响其纵横比?

【问题讨论】:

    标签: image qt qml qtquick2 qimage


    【解决方案1】:

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

    fillMode: Image.PreserveAspectFit
    

    【讨论】:

    • 正是我想要的。我很久以前就知道这个属性。缺少设置这个权利。非常感谢!
    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2014-03-13
    • 1970-01-01
    • 2010-12-03
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多