Window {
visible: true
width: 640
height: 480
title: qsTr("Window")
Rectangle{
width: 100
height: 100
color: "green"
Text {
text: qsTr("测试鼠标点击")
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons
onClicked: (mouse)=>
{
if (mouse.button === Qt.LeftButton) {
print("LeftButton")
if (mouse.buttons & Qt.RightButton) {
print("LRightButton") // 左键按下时右键按下
}
if ((mouse.buttons & Qt.RightButton) && (mouse.buttons & Qt.MiddleButton)) { // 左 中 右 一起
print("LMRightButton")
}
}
else if (mouse.button === Qt.RightButton) {
print("RightButton")
}
// 鼠标按下时,键盘功能键是否按住
if ((mouse.modifiers & Qt.ControlModifier) && (mouse.modifiers & Qt.AltModifier)) {
print("Ctrl + Alt")
}
}
onDoubleClicked: (mouse)=>{
print("double clicked")
}
onPressed: (mouse)=>{
print("pressed")
}
onReleased: (mouse)=>{
print("release")
}
}
}
// 鼠标移动事件
Rectangle {
x: 120
width: 100
height: 100
color: "blue"
Text {
text: qsTr("测试移动")
}
MouseArea {
hoverEnabled: true // 鼠标没有按下直接进入
anchors.fill: parent
acceptedButtons: Qt.AllButtons
onEntered: {
print("onEntered" + "containsMouse:" + containsMouse + " containsPress:" + containsPress)
parent.color = "red"
}
onExited: {
parent.color = "blue"
}
onPositionChanged: (mouse)=>{
print(containsPress + " " + mouse.x + " " + mouse.y)
}
onPressAndHold: (mouse)=>{
print(containsPress)
}
}
}
Rectangle {
id: container
y: 120
width: 600
height: 100
color: "black"
Rectangle {
id: dragrect
y: 25
width: 50
height: 50
color: "red"
MouseArea {
anchors.fill: parent
drag.target: dragrect
// 限制在x轴拖动
drag.axis: Drag.XAxis
// 限制拖动位置
drag.minimumX: 0
drag.maximumX: container.width - parent.width
}
}
}
}
评论