Qt 无法将double分配给QQuickAnchorLine

Qt 无法将double分配给QQuickAnchorLine,qt,qml,qt5,Qt,Qml,Qt5,我想把底部图像略高于顶部图像。但在向锚点添加偏移后,我得到以下错误: qrc:/Template.qml:21:22: Unable to assign double to QQuickAnchorLine 在此行上添加20个像素的偏移量后: anchors.top: top_bg.bottom-20 如何使用锚点作为参考点来偏移项目? import QtQuick 2.11 import QtQuick.Controls 2.4 import QtQuick.Layouts 1.11 im

我想把底部图像略高于顶部图像。但在向锚点添加偏移后,我得到以下错误:

qrc:/Template.qml:21:22: Unable to assign double to QQuickAnchorLine
在此行上添加20个像素的偏移量后:

anchors.top: top_bg.bottom-20
如何使用锚点作为参考点来偏移项目?

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.11
import QtQuick.Dialogs 1.0
import QtQuick.Controls.Material 2.3

Item {
    id: root
    Image {
        id: top_bg
        source: "qrc:/images/top_bg.png"
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        height: root.height*0.33
        fillMode: Image.PreserveAspectCrop
    }
    Image {
        id: map_bg
        source: "qrc:/images/map_bg.png"
        anchors.top: top_bg.bottom-20
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        fillMode: Image.PreserveAspectCrop

    }
}

锚定不是数字位置,而是相对于项目的位置,它们不能被添加或减去,在您的情况下,您应该使用属性
y
高度

Image {
    id: map_bg
    source: "qrc:/images/map_bg.png"
    y: top_bg.y + top_bg.height - 20 // <---
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.bottom: parent.bottom
    fillMode: Image.PreserveAspectCrop
}

您得到错误只是因为
锚定。bottom
不是一个数字,而是
锚定线
,事实上它是某种类型的枚举,因此您无法对此进行计算。但是你可以用边距代替。因此,在您的情况下,它应该是
anchors.top:top\u bg.bottom;anchors.topMargin:20
。阅读文章以获取更多信息。问题是,使用
y
x
对于必须在任何分辨率上运行的可扩展Qt应用程序(包括4K手机)都不好。以下是QT伸缩性说明中关于使用
x
y
的说明:“不要绑定到布局中项目的x、y、宽度或高度属性,因为这会与布局的目标冲突,也会导致绑定循环。”。因为我正在构建一个可重用的可伸缩组件,它可能包含在布局中。@Nulik我已经回顾了它所说的内容,但这些建议有上下文,使用Qt快速布局,而不是直接链接:动态布局屏幕控件…这里有一些与布局无关的事情:-不绑定x、y、宽度,或者布局中项目的高度属性,因为这会与布局的目标冲突,也会导致绑定循环。@Nulik[cont.]很明显,问题是可能会导致无限循环,因为存在双重依赖性,布局使用项目的大小来确定位置,并使用属性x、y、width,高度您可能也会这样做,可能会进入循环。@Nulik因此,如果未使用Qt快速布局,则第一个选项是正确的,并且在我更新的响应中,我添加了第二个使用边距的解决方案
Image {
    id: map_bg
    source: "qrc:/images/map_bg.png"
    anchors.top: top_bg.bottom
    anchors.topMargin: -20
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.bottom: parent.bottom
    fillMode: Image.PreserveAspectCrop
}