在此代码部分中,我创建了内部包含窗口和矩形的简单示例。我为矩形设置 x = 100 和 y = 100 内部坐标。当我将这些坐标映射到全局并得到不正确的结果时,坐标总是比实际坐标多 2 倍。例如,如果我将 x 和 y 设置为 50,则结果将为 50*2 = 100。为什么会出现错误?我该如何修复它?
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
id: root
width: 640; height: 480; visible: true;
title: qsTr("Hello World")
onXChanged: innerRect.globalChanged();
onYChanged: innerRect.globalChanged()
onWidthChanged: innerRect.globalChanged();
onHeightChanged: innerRect.globalChanged()
Rectangle {
id: innerRect
signal globalChanged()
property point globalPoint
onGlobalChanged: {
innerRect.globalPoint = mapToGlobal(innerRect.x, innerRect.y)
}
color: "yellow"
width: parent.width / 2; height: parent.height / 2
x: 100; y: 100
Text {
id: xCoord
anchors.left: parent.left;
anchors.leftMargin: 20
anchors.verticalCenter: parent.verticalCenter
text: "Gx: " + innerRect.globalPoint.x + " Wx: " + root.x
}
Text {
id: yCoord
anchors.right: parent.right;
anchors.rightMargin: 20
anchors.verticalCenter: parent.verticalCenter
text: "Gy: " + innerRect.globalPoint.y + " Wy: " + root.y
}
}
}