mapFromItem##
object mapFromItem(Item item, real x, real y)###
將參數(shù)item(A)的坐標(biāo)系統(tǒng)的坐標(biāo)點(diǎn)(x,y)映射到調(diào)用該方法的對(duì)象(B)坐標(biāo)系統(tǒng)中,并返回一個(gè)映射之后的坐標(biāo)(point)。此時(shí),參數(shù)x,y是指的A對(duì)象上的坐標(biāo)點(diǎn)。如果item參數(shù)是null值,坐標(biāo)點(diǎn)的值就是從QML視圖的根元素的坐標(biāo)系統(tǒng)映射出來(lái)的。
object mapFromItem(Item item, real x, real y, real width, real height)###
將參數(shù)item(A)的坐標(biāo)系統(tǒng)的區(qū)域(x,y,width,height)映射到調(diào)用該方法的對(duì)象(B)的坐標(biāo)系統(tǒng)中,并返回一個(gè)映射之后的區(qū)域(rect)。此時(shí),參數(shù)x,y,width,height是指來(lái)自的A對(duì)象的區(qū)域。如果item參數(shù)是null值,坐標(biāo)點(diǎn)的值就是從QML視圖的根元素的坐標(biāo)系統(tǒng)映射出來(lái)的。
mapToItem##
object mapToItem(Item item, real x, real y)###
將調(diào)用該方法的對(duì)象(C)的坐標(biāo)點(diǎn)(x,y),映射到參數(shù)item(D)的坐標(biāo)系統(tǒng)中,并返回一個(gè)映射之后的坐標(biāo)(point)。此時(shí),參數(shù)x,y是指的C對(duì)象上的坐標(biāo)點(diǎn)。如果item參數(shù)是null值,坐標(biāo)點(diǎn)的值就是從QML視圖的根元素的坐標(biāo)系統(tǒng)映射出來(lái)的。
object mapToItem(Item item, real x, real y, real width, real height)###
將調(diào)用該方法的對(duì)象(C)的區(qū)域(x,y,width,height),映射到參數(shù)item(D)的坐標(biāo)系統(tǒng)中,并返回一個(gè)映射之后的區(qū)域(rect)。此時(shí),參數(shù)x,y,width,height是指來(lái)自的C對(duì)象的區(qū)域。如果item參數(shù)是null值,坐標(biāo)點(diǎn)的值就是從QML視圖的根元素的坐標(biāo)系統(tǒng)映射出來(lái)的。
再看一個(gè)使用實(shí)例:
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
x: 100
y: 100
width: 640
height: 480
title: qsTr("Test item maps")
Rectangle {
id: container
anchors.fill: parent
anchors.margins: 20
border.color: "gray"
Rectangle {
id: content
implicitWidth: 120
implicitHeight: 120
color: "#666"
Text {
id: posValue
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
drag.target: content
drag.minimumX: 0
drag.maximumX: container.width - content.width
drag.minimumY: 0
drag.maximumY: container.height - content.height
function updateText(){
var t,m
m = container.mapFromItem(content, 0, 0, 12, 34);
t = m.x + "," + m.y + "," + m.width + "," + m.height + "\n"
m = container.mapFromItem(null, 0, 0, 12, 34);
t += m.x + "," + m.y + "," + m.width + "," + m.height + "\n"
m = content.mapToItem(container, 0, 0, 12, 34);
t += m.x + "," + m.y + "," + m.width + "," + m.height + "\n"
m = content.mapToItem(null, 0, 0, 12, 34);
t += m.x + "," + m.y + "," + m.width + "," + m.height
posValue.text = t
}
onPositionChanged: {
updateText()
}
Component.onCompleted: updateText()
}
}
}
}
從代碼以及運(yùn)行效果,我們可以大概的了解這兩個(gè)function的使用場(chǎng)景和使用之后的效果。
以上是個(gè)人對(duì)這兩個(gè)function的理解,如有不當(dāng)之處歡迎留言指正。