本页内容

概念 - 视觉坐标在Qt Quick

项目坐标

Qt Quick 中默认使用的视觉坐标系是项目坐标系。这是一个笛卡尔坐标系,原点 (0,0) 位于项目的左上角。x 轴向右延伸,y 轴向下延伸,因此项目的右下角位于坐标 (width, height) 处。

单个项的位置是相对于其父项的坐标系来指定的。这意味着从非同级项中读取 x、y 值时,可能需要进行转换,将其转换为相同的坐标系。此时,场景坐标通常被用作中间坐标系。

场景坐标

场景坐标系中,(0,0) 对应于当前渲染场景的窗口左上角。场景坐标通常与窗口中根项的项坐标一致。

您可以使用目标对象上的相关函数,将对象坐标转换为场景坐标。有关转换为场景坐标或另一个对象坐标的方法,请参阅Item::mapFromItem 和Item::mapToItem 。

示例

下面的 QML 代码创建了一个由正方形组成的布局,并添加了用于标识点的圆点:

Rectangle {
    width: 200
    height: 200
    color: "red"

    Rectangle {
        x: 100
        y: 100
        width: 100
        height: 100
        color: "blue"

        Rectangle {
            width: 50
            height: 50
            color: "green"
        }
    }
}

红色矩形内嵌有蓝色和绿色矩形,显示坐标位置

在此图中,黑色圆点位于红色矩形项目坐标系内的 (0,0) 位置。如果红色矩形是场景的根项,那么黑色圆点在场景坐标系中的位置也是 (0,0)。

蓝色矩形位于白色圆点处,即 (100,100),该位置相对于红色矩形的左上角。

绿色矩形未指定 x、y 坐标,因此其位置默认为 (0,0)。由于它在父对象(蓝色矩形)的坐标系中位于 (0,0),因此它位于该矩形的左上角。 该点与红色矩形坐标系中白色圆点 (100,100) 位于同一位置。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.