import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1
Window
{
id:root
visible: true
width: 600
height: 600
RowLayout
{
id: layout
anchors.fill: parent
spacing: 0
Rectangle
{
id:rect
color: "#ff0000"
width: root.width
height: root.height/2
y:root.height/2
MouseArea
{
id:rect_mouse
anchors.fill: parent
hoverEnabled: true //是否处理悬浮事件,默认false,只有按下鼠标键时才处理鼠标事件,为true时即使没有按下鼠标键也会作相应的处理
preventStealing:true//默认为false,为true时可以防止当前鼠标事件被其它对象拦截。
propagateComposedEvents:true//默认为 false,当设置为 true 时,就可以将事件传递给重叠的其他鼠标区域了
enabled: true
cursorShape: Qt.SplitHCursor//鼠标进入区域鼠标指针变成手型
onPositionChanged:
{
var positionInRoot = rect.mapToItem(rect, mouse.x, mouse.y)
var positionInWindow = rect.mapToItem(root.contentItem, mouse.x, mouse.y)
var globalPosition = root.contentItem.mapToGlobal(mouse.x, mouse.y)
console.log("到当前Rectangle左上角的距离" + positionInRoot )
console.log("到窗口左上角的距离" + positionInWindow)
console.log("到屏幕左上角的距离" + globalPosition)
}
}
}
}
}