概念 - 视觉坐标Qt Quick
项坐标
Qt Quick 中使用的默认视觉坐标系是项目坐标系。这是一个直角坐标系,(0,0) 位于项目的左上角。x 轴向右延伸,y 轴向下延伸,因此项目的右下角坐标为(宽,高)。
单个项目的位置是根据其父级坐标系指定的。这意味着读取非同级项目的 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) 处的白点是同一个点。
© 2025 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.