调整QML图像显示大小

Phr*_*ogz 6 qt qml qtquick2

我有一个嵌套的QML窗口RowLayout.在内排我有两个图像..png这些图像的源文件(有意)相当大.当我尝试height在这些图像上设置属性以使它们变小时,它们仍然被绘制得很大.

期望的外观:
两个并排的图像,占窗口高度的1/3

实际外观:
两个并排的图像,远大于它们所需的尺寸

我能够让它们变小的唯一方法是设置sourceSize.height:100而不是height:100; 然而,这不是我想要的.我希望他们能够在不重新加载的情况下向上和向下扩展.

如何修复我的QML以使图像占据其包含的高度RowLayout

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

ApplicationWindow {
  width:600; height:300
  visible:true

  Rectangle {
    color:'red'
    anchors { top:header.bottom; bottom:footer.top; left:parent.left; right:parent.right }
  }

  header:RowLayout {
    id:header
    spacing:0
    height:100; width:parent.width

    RowLayout {
      id:playcontrol
      Layout.minimumWidth:200; Layout.maximumWidth:200; Layout.preferredWidth:200
      height:parent.height
      Image {
        // I really want these to take on the height of their row
        source:'qrc:/img/play.png'
        width:100; height:100
        fillMode:Image.PreserveAspectFit; clip:true
      }
      Image {
        source:'qrc:/img/skip.png'
        width:100; height:100
        fillMode:Image.PreserveAspectFit; clip:true
      }
    }

    Rectangle {
      color:'#80CC00CC'
      Layout.minimumWidth:200
      Layout.preferredWidth:parent.width*0.7
      Layout.fillWidth:true; Layout.fillHeight:true
      height:parent.height
    }
  }

  footer:Rectangle { height:100; color:'blue' }
}
Run Code Online (Sandbox Code Playgroud)

Mit*_*tch 11

使用布局时,请勿指定项目widthheight项目; 请改用Layout附加的属性.布局本身将设置widthheight,有效地覆盖您设置的任何内容.

因此,对于您的图像,请替换

width:100; height:100
Run Code Online (Sandbox Code Playgroud)

Layout.preferredWidth: 100
Layout.preferredHeight: 100
Run Code Online (Sandbox Code Playgroud)

在此处记录.具体来说,widthheight仅作为一种"最后的回退",他们将不会像你期望.

您的代码中还有其他地方发生这种情况:

  • playcontrol集合height: parent.height(填充父级的宽度和高度是布局默认行为,因此无论如何这都不是必需的).
  • Rectangle范围内的playcontrol布局也设置height: parent.height.