版本
Qt5.15
相关函数
下面这些函数是隶属于Item的
/// Item
object mapFromGlobal(real x, real y)
object mapFromItem(Item item, rect r)
object mapFromItem(Item item, real x, real y, real width, real height)
object mapFromItem(Item item, point p)
object mapFromItem(Item item, real x, real y)
object mapToGlobal(real x, real y)
object mapToItem(Item item, rect r)
object mapToItem(Item item, real x, real y, real width, real height)
object mapToItem(Item item, point p)
object mapToItem(Item item, real x, real y)
值得注意的是mapFromGlobal
和mapToGlobal
的使用版本是自Qt5.7开始的
而mapFromItem
和mapToItem
是自Qt 5.15开始的
mapFrom和mapTo,
前者是指从被指向的item坐标系里的坐标参数,映射到使用者坐标系里的坐标是如何;
后者是指使用者坐标系里的坐标参数,映射到被指向的item坐标系里的坐标是如何。
打个调用示例:
Rectangle {
id: rect1
width: 100
height: 100
...
Button {
id: button1
x: 50
y: 52
width: 30
height: 30
...
...
mapToItem
button1.mapToItem(rect1, 10, 12)
该函数用法是button1里QPointF(10, 12)这个坐标,在rect1里坐标是QPointF(0 + 50 + 10, 0 + 52 + 12)
官方解释:
Maps the point (x, y) or rect (x, y, width, height), which is in this item’s coordinate system, to item’s coordinate system, and returns a point or rect matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
If item is a null value, this maps the point or rect to the coordinate system of the root QML view.
The versions accepting point and rect are since Qt 5.15.
有道翻译:
将位于该项坐标系统中的点(x, y)或矩形(x, y,宽度,高度)映射到该项的坐标系统,并返回与映射坐标匹配的点或矩形。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
如果item为空值,则将点或矩形映射到根QML视图的坐标系统。
接受point和rect的版本是从Qt 5.15开始的。
mapFromItem
button1.mapFromItem(rect1, 10, 12)
该函数用法是rect1里QPointF(10, 12)这个坐标,在button1里坐标是QPointF(0 - 50 + 10, 0 - 52 + 12)
官方解释
:
Maps the point (x, y) or rect (x, y, width, height), which is in item’s coordinate system, to this item’s coordinate system, and returns a point or rect matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
If item is a null value, this maps the point or rect from the coordinate system of the root QML view.
The versions accepting point and rect are since Qt 5.15.
有道翻译
:
将项目坐标系统中的点(x, y)或矩形(x, y,宽度,高度)映射到该项目的坐标系统,并返回与映射坐标匹配的点或矩形。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
如果item为空值,则从根QML视图的坐标系统映射点或矩形。
接受point和rect的版本是从Qt 5.15开始的。
mapToGlobal与mapFromGlobal
button1.mapToGlobal(10, 12)
button1.mapFromGlobal(10, 12)
上面这两个与上同理,只是换成了Global,这个Global的对应对象就是根节点的Item所在坐标系,不过要注意的
是Window这个根节点,其的坐标系是整个屏幕的坐标系,而不是窗口的坐标系
mapToGlobal官方解释
:
Maps the point (x, y), which is in this item’s coordinate system, to the global coordinate system, and returns a point matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
This method was introduced in Qt 5.7.
有道翻译
:
将位于该项坐标系统中的点(x, y)映射到全局坐标系统,并返回与映射坐标匹配的点。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
这种方法是在Qt 5.7中引入的。
mapFromGlobal官方解释
:
Maps the point (x, y), which is in the global coordinate system, to the item’s coordinate system, and returns a point matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
This method was introduced in Qt 5.7.
有道翻译
:
将全局坐标系中的点(x, y)映射到项目的坐标系,并返回与映射坐标匹配的点。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
这种方法是在Qt 5.7中引入的。
相关小技巧
button1.mapToItem(null, 10, 12)
该函数用法是button1里QPointF(10, 12)这个坐标,在整个窗口里的坐标是如何