我想将底部图像稍微放在顶部图像上方。但是在向锚添加偏移量后,出现此错误:
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
}
}
答案 0 :(得分:2)
锚点不是数字位置,而是相对于项目的位置,不能相加或相减,在这种情况下,应使用属性y
和height
:
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
}
另一种选择是使用边距:
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
}