如何获取ColumnLayout中元素的笛卡尔坐标



有一个ColumnLayout,里面有4个不同的项,使用Layout定位,我如何从其中一个项获得笛卡尔坐标?

示例:

import QtQuick 2.11
import QtQuick.Layouts 1.3
ColumnLayout {
spacing: 0
Layout.topMargin: 10
Layout.bottomMargin: 10
Image {
id: item1 
Layout.alignment: Qt.AlignHCenter
sourceSize: Qt.size(204, 96)
}
Text {
id: item2
Layout.topMargin: 15
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
text: "hello"
}
Text {
id: item3
Layout.alignment: Qt.AlignHCenter
text: "world"
}
}

id为"item3"的元素在笛卡尔坐标系中的位置是什么?如何获得?

如果要获得item3相对于ColumnLayout的位置,则必须使用属性x,y:

// ...
Text {
id: item3
Layout.alignment: Qt.AlignHCenter
text: "world"
Component.onCompleted: console.log(x, y)
}

如果你想尊重屏幕,你必须使用mapToGlobal((函数:

Component.onCompleted: console.log(mapToGlobal(x, y))

最新更新