Qt 如何使用QML布局在网格中排列纵横比缩放项目?

Qt 如何使用QML布局在网格中排列纵横比缩放项目?,qt,layout,qml,aspect-ratio,layout-anchor,Qt,Layout,Qml,Aspect Ratio,Layout Anchor,我有长宽比一定的矩形和长宽比相反的矩形。我想将它们安排在我选择的网格布局中(不需要是常规网格,相反:我更喜欢一个可以随意构建RowLayouts和ColumnLayouts的解决方案) 我知道我可以使用Layout.fillHeight和Layout.fillWidth在布局中设置缩放项目。不幸的是,我找不到合适的方法来定义我的矩形的纵横比。我知道QMLImage可以做到这一点(通过它的fillMode属性),但我认为没有简单的方法可以很好地做到这一点 任何正确方向的帮助或指点都将不胜感激 注意

我有长宽比一定的矩形和长宽比相反的矩形。我想将它们安排在我选择的网格布局中(不需要是常规网格,相反:我更喜欢一个可以随意构建
RowLayout
s和
ColumnLayout
s的解决方案)

我知道我可以使用
Layout.fillHeight
Layout.fillWidth
在布局中设置缩放项目。不幸的是,我找不到合适的方法来定义我的
矩形的纵横比。我知道QML
Image
可以做到这一点(通过它的
fillMode
属性),但我认为没有简单的方法可以很好地做到这一点

任何正确方向的帮助或指点都将不胜感激

注意,我假设QML布局是可行的方法,但是如果有一个功能解决方案只包含锚或简单的
/
设置,我完全赞成

还要注意的是,我更愿意保持两种类型的
矩形的面积相同,就像在实验中看到的那样,这并不是那么简单

编辑

我所说的尝试,减去等面积限制。矩形填充宽度,但在高度中保留空间,因为它们受纵横比和填充宽度的约束。高度也应该是一样的,但我无法将两者结合起来


您可以使用属性绑定将矩形的
宽度
高度
绑定,具体操作如下:

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Layouts 1.0

Window {
    id: root
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    property int minWidth: 150
    property int maxWidth: 300
    property int minHeight: 150
    property int maxHeight: 250
    property int rowWidth: root.width/3 //Change accordingly the width of each child item w.r.t. the width of the root.
    Row {
        id: layout
        anchors.fill: parent
        spacing: 6
        Rectangle {
            color: 'red'
            // 4/3 is the aspect ratio of the first Rectangle
            height: (3*width/4)<root.minHeight ? root.minHeight : ( (3*width/4)>root.maxHeight ? root.maxHeight : 3*width/4 )
            width: (root.rowWidth) < root.minWidth ? root.minWidth : ( root.rowWidth > root.maxWidth ? root.maxWidth : root.rowWidth)
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
        Rectangle {
            color: 'green'
            // 3/4 is the aspect ratio of the second Rectangle
            height: (4*width/3)<root.minHeight ? root.minHeight : ( (4*width/3)>root.maxHeight ? root.maxHeight : 4*width/3 )
            width: (root.rowWidth) < root.minWidth ? root.minWidth : ( root.rowWidth > root.maxWidth ? root.maxWidth : root.rowWidth)
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
    }
}
导入QtQuick 2.5
导入QtQuick.Window 2.2
导入QtQuick.Layouts 1.0
窗口{
id:根
可见:正确
宽度:640
身高:480
标题:qsTr(“你好世界”)
属性int最小宽度:150
属性int最大宽度:300
属性int最小高度:150
属性int最大高度:250
属性int rowWidth:root.width/3//相应地更改每个子项的宽度w.r.t.根的宽度。
划船{
id:布局
锚定。填充:父级
间距:6
长方形{
颜色:“红色”
//4/3是第一个矩形的纵横比
高度:(3*width/4)root.maxHeight?root.maxHeight:3*width/4)
宽度:(root.rowWidth)root.maxWidth?root.maxWidth:root.rowWidth)
正文{
anchors.centerIn:父对象
文本:parent.width+'x'+parent.height
}
}
长方形{
颜色:“绿色”
//3/4是第二个矩形的纵横比
高度:(4*width/3)root.maxHeight?root.maxHeight:4*width/3)
宽度:(root.rowWidth)root.maxWidth?root.maxWidth:root.rowWidth)
正文{
anchors.centerIn:父对象
文本:parent.width+'x'+parent.height
}
}
}
}

几天来,我一直在努力寻找正确的方法。 我找不到任何工作的例子,所以我写了我自己的

此矩形将始终遵循其目标比例,并保留其边距。 这里的诀窍是处理不同的情况:要么父母的比例大于目标父母的比例,要么不大于目标父母的比例。在一种情况下,将宽度绑定到父对象,并根据设置高度。在另一种情况下,你用另一种方式做

Rectangle {
    color  : 'green'

    // INPUTS
    property double  rightMargin    : 20
    property double  bottomMargin   : 20
    property double  leftMargin     : 20
    property double  topMargin      : 20
    property double  aimedRatio     : 3/4

    // SIZING
    property double  availableWidth  : parent.width  - rightMargin  - leftMargin
    property double  availableHeight : parent.height - bottomMargin - topMargin

    property bool    parentIsLarge   : parentRatio > aimedRatio

    property double  parentRatio     : availableHeight / availableWidth

    height : parentIsLarge ? width * aimedRatio :  availableHeight
    width  : parentIsLarge ? availableWidth     :  height / aimedRatio

    anchors.top        : parent.top
    anchors.topMargin  : topMargin
    anchors.left       : parent.left
    anchors.leftMargin : leftMargin
}

希望它能帮助那些通过谷歌搜索来到这里的人们

你能提供一个描述你想要达到的目标的图片吗?这将有助于理解并有希望回答您的问题。@GrecKo这一尝试涵盖了我的三分之一的限制。我给你看一个满意的;-)。作为一张图片,我指的是一张能够满足所有约束条件的图片,它可以在画图中完成,而不必在QML中完成,否则就不需要太多帮助;)好吧,我需要几个来真正展示它们。我以后再做!如果布局无法满足您的需求,您可以查看一下。我还没有真正使用它,所以我不能提供更多的细节,但它似乎是一个很好的线索。这并没有保持矩形的纵横比。我只得到固定在边缘的矩形。@rubenvb调整窗口大小时,宽高比保持不变。这不是您想要的吗?我在将代码复制到新的Qt Creator QML应用程序时没有看到这种行为:/。我只能告诉你这些。哦,这是在Mac电脑上的,问题中提到的Qt5.5。不确定这些是否相关。你能告诉我你得到的确切输出是什么,或者它与你想要的有什么不同吗?